Aggregation and GROUP BY

Summarise many rows into counts, sums and averages.

Notes

Aggregates such as COUNT, SUM and AVG collapse rows into one value per group.

WHERE filters before grouping; HAVING filters groups afterwards.

Key points

  • Every non-aggregated column goes in GROUP BY.
  • Use HAVING for conditions on aggregates.
  • COUNT(DISTINCT x) counts unique values.