Aggregation
Aggregation
How to summarise rows — `GROUP BY`, the eight aggregate functions you'll use 90% of the time, and the multi-dimensional aggregation tools (`ROLLUP`, `CUBE`, `GROUPING SETS`) that produce subtotals in a single query.
Suggest an editAggregation
A SELECT over customers returns one row per customer. A SELECT plus GROUP BY country returns one row per country, with the per-country aggregates (count, sum, average, min, max) computed across that country's rows. Aggregation is the operation that turns "many rows" into "one row per group" — the language for summarising data instead of listing it.
This module covers the three pieces: the mechanics of GROUP BY and HAVING, the catalogue of aggregate functions and their non-obvious corners (especially around NULL), and the multi-dimensional grouping operators (ROLLUP, CUBE, GROUPING SETS) that compute subtotals at multiple levels in one query.
Place in the curriculum
- Prerequisites: Foundations and Joins.
GROUP BYis a step in the logical execution order, and most aggregation queries operate on joined tables. - Followed by: Window Functions. Window functions generalise aggregation — you can keep one row per original row while computing aggregates across a window of related rows.
Chapters
- GROUP BY and HAVING — the mechanics: how rows collapse into groups, what columns are legal in
SELECTafter aGROUP BY, and the difference betweenWHERE(filters rows before grouping) andHAVING(filters groups after). - Aggregate Functions —
COUNT,SUM,AVG,MIN,MAX,STRING_AGG/GROUP_CONCAT, plus the modifiers (DISTINCT,FILTER) that change their meaning. Plus the NULL behaviour that surprises everyone the first time. - Grouping Sets, ROLLUP, CUBE — multi-dimensional aggregation in a single query: subtotals by country, subtotals by month, subtotals by both. The right tool for spreadsheet-style "what does the data look like at every level."