GROUP BY and aggregates
Collapse many rows into one summary row per group.
Open this lesson in the learning hubKey points
- An aggregate -
COUNT,SUM,AVG,MIN,MAX- turns a set of rows into one value. GROUP BY citymakes one bucket per city, then aggregates each bucket separately.COUNT(*)counts rows;COUNT(col)skips NULLs in that column.- Every non-aggregated column you select must be in the GROUP BY - otherwise the value is arbitrary.
- Filter rows before grouping with WHERE, and groups after grouping with HAVING.
GROUP BY makes buckets; the aggregate reduces each bucket to a single number.
This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the MySQL Course course, and every lesson in it is listed on the MySQL Course contents page.