Track 0/9 (0%)
Module 04 · Aggregation~20 min · 6 worked queries
Grouping, Folding & HAVING Filters
Collapse surviving rows into keyed buckets with GROUP BY, distinguish COUNT(*) from COUNT(col), and filter post-fold buckets with HAVING.
WHERE discards individual tuples before grouping; GROUP BY folds N tuples per key into 1 summary row; HAVING discards whole buckets afterward.Module 04 · Aggregation · Reading Room
Mental Model
- 01.
Every non-aggregated column in SELECT must belong to the GROUP BY key list so group identity stays unambiguous.
- 02.
COUNT(*) counts rows in the bucket including NULLs; COUNT(col) counts only rows where col IS NOT NULL.
- 03.
Conditional aggregation — SUM(CASE WHEN ... THEN 1 ELSE 0 END) — computes pivot counts inside a single scan.
Interactive Module Lab · Aggregations
22. HAVING filters groups — sqlSELECT department_id, COUNT(*) AS total_staff, AVG(salary) AS avg_salaryFROM usersWHERE department_id IS NOT NULLGROUP BY department_idHAVING COUNT(*) > 1;
Execution Stages4 steps
Stage 01 · FROM
10 in → 10 outSOURCE: USERS
Scanned 10 rows from table 'users'.
| # | id | name | department_id | role | salary | state |
|---|---|---|---|---|---|---|
| 1 | 1 | Alice Smith | 1 | Software Engineer | 95,000 | keep |
| 2 | 2 | Bob Jones | 2 | Product Manager | 110,000 | keep |
| 3 | 3 | Charlie Lee | 1 | Software Engineer | 88,000 | keep |
| 4 | 4 | Diana Prince | 3 | UX Designer | 76,000 | keep |
| 5 | 5 | Evan Wright | 2 | Data Analyst | 82,000 | keep |
| 6 | 6 | Fiona Gallagher | 1 | Tech Lead | 135,000 | keep |
| 7 | 7 | George Clark | NULL | Intern | 42,000 | keep |
| 8 | 8 | Hannah Abbott | 2 | Scrum Master | 89,000 | keep |
Inspect each evaluated clause in 22. HAVING filters groups or animate every row moving between stages in Studio.Open 22. HAVING filters groups in Studio →
Worked Presets in This Module
20. COUNT(*) vs COUNT(column)
COUNT(*) counts rows; COUNT(column) skips NULLs; COUNT(DISTINCT ...) skips repeats. No GROUP BY means one group.
21. GROUP BY & Aggregations
Rows are sorted into buckets by department, then each bucket collapses into a single summary row.