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.

January 22nd, 2009

GROUP BY

Posted by admin in K. Grouping and Filtering

The GROUP BY clause divides a table into logical groups.
The order of multiple columns in the GROUP BY clause specifies the grouping level from the highest to the lowest level of grouping.
Only one row results for each distinct value in the grouping values.
Each row in the result contains summary data of the values in the grouping columns.

                                  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 ...];

> The GROUP BY clause is not necessary unless used with a column function.

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

> The WHERE clause can eliminate data from consideration by the GROUP BY clause.

> A column name in the GROUP BY clause must match a column name in the SELECT statement. Column names in the GROUP BY clause do not have to be in the same order as the SELECT statement. If the SELECTed name is qualified, the GROUP BY name must be qualified.

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

> If a grouping column contains a NULL or NULLs, the NULLs become a group.

> To sort the GROUP BY groups, use the ORDER BY clause.

> Without the ORDER BY clause, the groups returned by the GROUP BY clause can be in any order. In some RDBMS Systems, GROUP BY implies ORDER BY.

> Multiple grouping columns in the GROUP BY clause creates nested groups.

> Table AS aliases are allowed as qualifiers, but AS aliases can’t be used in the GROUP BY clause.
See Table Aliases (AS).

> If you use a column for GROUP BY frequently, that column should have an index.

> If used without a column function, GROUP BY is the same as DISTINCT.

> RDBMS systems such a MySQL and PostgreSQL allow column aliases in the GROUP BY clause.