GROUP BY
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.


