GROUP BY
Group rows by one or more columns and apply aggregate functions to each group:HAVING
Filter groups after aggregation. You can use both aggregate and non-aggregate predicates:Aggregate Functions
| Function | Description |
|---|---|
COUNT(*) | Count all rows in the group |
COUNT(col) | Count non-NULL values in col |
SUM(col) | Sum of non-NULL values |
AVG(col) | Average of non-NULL values |
MIN(col) | Minimum value |
MAX(col) | Maximum value |
STDDEV(col) | Population standard deviation |
DISTINCT Aggregates
Deduplicate values before aggregating:Using Aliases in ORDER BY
Aliases defined in theSELECT list are available in ORDER BY. They cannot be used in GROUP BY or HAVING — repeat the expression there instead.
Not Supported
GROUP BYwith expressions (e.g.,GROUP BY YEAR(created_at))- Positional
GROUP BYordinals (e.g.,GROUP BY 1, 2) GROUPING SETS,ROLLUP,CUBE- Window functions (
ROW_NUMBER,RANK,SUM OVER, etc.)