Module 4: Aggregates and GROUP BY
Summarise thousands of rows into a report: totals, averages and counts per group, filtered with HAVING.
- COUNT, SUM, AVG, MIN, MAX: COUNT() counts rows, COUNT(col) skips NULLs, COUNT(DISTINCT col) counts different values; AVG ignores NULLs; SUM of no rows is NULL.
- GROUP BY one and many columns: GROUP BY makes one row per bucket; every plain column in SELECT must be in GROUP BY; NULLs form one group.
- HAVING vs WHERE, and the order a query runs in: WHERE filters rows before grouping; HAVING filters groups after; the logical order is FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT.
- Conditional aggregation: SUM(CASE WHEN … THEN 1 ELSE 0 END) or COUNT() FILTER (WHERE …) counts some rows per group; one per column makes a pivot.
Ends with a 13-question checkpoint (80% passes) and a project: The restaurant scorecard.