GROUP BY and aggregates

MySQL Course · lesson 5 of 21 · 5 min read

Collapse many rows into one summary row per group.

Open this lesson in the learning hub

Key points

  • An aggregate - COUNT, SUM, AVG, MIN, MAX - turns a set of rows into one value.
  • GROUP BY city makes 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.