Skip to main content
Albero Academy

GROUP BY

Aggregate per category — the analytical workhorse.

Core Queries9 min

GROUP BY turns aggregate functions from "one number for the whole table" into "one number per group". It's the analytical workhorse — "average package per programme", "orders per month".

Grouping

one row per group
SELECT programme, COUNT(*) AS n, AVG(package) AS avg_pkg
FROM students
GROUP BY programme;
SELECT programme, year, AVG(package) -- group by two columns
FROM students
GROUP BY programme, year;

The GROUP BY rule

Key takeaways

  1. 01GROUP BY produces one summary row per group.
  2. 02It turns aggregates into per-category answers (avg per programme, count per month).
  3. 03You can group by multiple columns.
  4. 04Every SELECT column must be grouped or aggregated.

Finished this chapter?

Mark it complete to track your progress through SQL.