GROUP BY Clause
The GROUP BY clause denotes that rows should be grouped according to the specified expression values. One row will be returned for each group, after optionally filtering those aggregate rows based on a HAVING clause.
The general form of the GROUP BY clause is as follows:
GROUP BY expression (,expression)
Syntax Rules:
- Column references in the
GROUP BYclause must apply to unaliased output columns; - Expressions used in the
GROUP BYclause must appear in theSELECTclause; - Column references and expressions in the select clause that are not used in the
GROUP BYclause must appear in aggregate functions; - If an aggregate function is used in the
SELECTclause and noGROUP BYis specified, an implicitGROUP BYwill be performed with the entire result set as a single group. In this case, every column in theSELECTclause must be an aggregate function as no other column value will be fixed across the entire group; - The
GROUP BYcolumns must be of a comparable type.