GROUP BY Clause
You are looking at an older version of the documentation. The latest version is found here.
The GROUP BY
clause denotes that rows should be grouped according to the specified expression values. Each group will return one row 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 BY
clause must apply to unaliased output columns; - Expressions used in the
GROUP BY
clause must appear in theSELECT
clause; - Column references and expressions in the select clause that are not used in the
GROUP BY
clause must appear in aggregate functions; - If an aggregate function is used in the
SELECT
clause and noGROUP BY
is specified, an implicitGROUP BY
will be performed with the entire result set as a single group. In this case, every column in theSELECT
clause must be an aggregate function as no other column value will be fixed across the entire group; - The
GROUP BY
columns must be of a comparable type.