January 23rd, 2009

HAVING

Posted by admin in K. Grouping and Filtering

Just as the WHERE clause limits the number of rows retrieved by the SELECT statement, the HAVING clause limits the number of groups retrieved for the GROUP BY clause.

                         SELECT      [ALL | DISTINCT]
                                           [* | column1[, column2, ...]]
                            FROM      [table1 correlation, ...
                                          | view1 correlation, ...]
                          WHERE       condition1[, condition2, ...]
                          GROUP BY  column1[, column2, ...]
                         HAVING      condition1[, condition2, ...]
                           ORDER BY column1 [ASC | DESC][, column2 [ASC | DESC], …];

> Unlike the WHERE clause, the HAVING clause can contain a column function.

> The HAVING clause comes after the GROUP BY clause and before the ORDER BY clause.

> The WHERE clause can eliminate data from consideration by the GROUP BY clause. The WHERE clause is applied before grouping happens. The HAVING clause is applied after grouping happens.

> An entry in the HAVING clause must match an entry in the SELECT statement or be a column function.
> Column names in the HAVING clause do not have to be in the same order as the SELECT statement.

> If the SELECTed name is qualified, the HAVING name must be qualified.

> An expression in the HAVING clause must match the exact same expression in the SELECT statement.

> Multiple HAVING conditions can be specified using AND, OR, and NOT.

Leave a reply

:mrgreen: :neutral: :twisted: :shock: :smile: :???: :cool: :evil: :grin: :oops: :razz: :roll: :wink: :cry: :eek: :lol: :mad: :sad: