Loading…
Loading…
Charting your lesson…
Opening your lesson…
Aggregate Functions, GROUP BY and HAVING
Lesson 3 of 4
6 steps•about 12 min•12 flashcards
Step 1/ 61 min
Aggregation turns many rows into summary numbers — counts, sums, averages per category. Mastering GROUP BY and the WHERE-vs-HAVING distinction is essential, both for real reporting queries and for exams.
Core idea: aggregate functions (COUNT, SUM, AVG, MIN, MAX) collapse a set of rows into one value. GROUP BY partitions rows into groups so aggregates are computed per group. WHERE filters rows before grouping; HAVING filters groups after aggregation.
Step 2/ 61 min
SELECT COUNT(*), AVG(salary), MAX(salary) FROM Employee;
Without GROUP BY, the whole table is one group → one summary row.
SELECT dept_id, COUNT(*) AS headcount, AVG(salary) AS avg_pay
FROM Employee
GROUP BY dept_id; -- one output row per department
Rule: every column in the SELECT list must be either inside an aggregate or in the GROUP BY. Selecting a non-grouped, non-aggregated column is an error (or, worse, silently arbitrary in lax databases).
The clauses execute in a fixed logical order, which explains many quirks:
FROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY
WHERE runs before grouping, so it filters individual rows and cannot reference aggregates.HAVING runs after grouping, so it filters groups and can use aggregates.SELECT runs after GROUP BY/HAVING, column aliases defined in SELECT aren't visible to WHERE/HAVING in standard SQL.SELECT dept_id, AVG(salary) AS avg_pay
FROM Employee
WHERE status = 'active' -- filter rows first
GROUP BY dept_id
HAVING AVG(salary) > 50000; -- then filter groups
Step 3/ 61 min
Data: dept 1 has salaries {60k, 40k}; dept 2 has {90k}; dept 3 has {30k, 30k}.
SELECT dept_id, COUNT(*) AS n, AVG(salary) AS avg
FROM Employee GROUP BY dept_id HAVING AVG(salary) > 45000;
HAVING AVG > 45000 keeps dept1 (50k) and dept2 (90k); drops dept3 (30k).WHERE AVG(salary) > 45000 instead would be an error, because WHERE runs before the average exists — the exact point HAVING exists to solve.Step 4/ 61 min
Very common: "difference between WHERE and HAVING", "what must appear in GROUP BY", "how does COUNT(*) differ from COUNT(column)" (the latter skips NULLs), and predicting query output. The logical processing order is the key that unlocks most of these.
Step 5/ 61 min
WHERE = row filter (pre-group, no aggregates); HAVING = group filter (post-group, aggregates allowed).COUNT(*) counts rows; COUNT(col) counts non-NULL values of col; COUNT(DISTINCT col) counts distinct non-NULLs.COUNT(*)) ignore NULLs — AVG(salary) divides by the count of non-NULL salaries.Putting an aggregate condition in WHERE, e.g. WHERE COUNT(*) > 5. Aggregates don't exist yet when WHERE runs (it filters raw rows before GROUP BY), so this is a syntax error. Aggregate-based group filtering must go in HAVING, which runs after grouping.
COUNT/SUM/AVG/MIN/MAX) collapse many rows into one value.GROUP BY computes aggregates per group; SELECT columns must be grouped or aggregated.WHERE filters rows before grouping (no aggregates); HAVING filters groups after (aggregates allowed).COUNT(*) counts rows; other aggregates and COUNT(col) ignore NULLs.GROUP BY does it per category. Filter individual rows with WHERE (before grouping) and whole groups with HAVING (after aggregation) — the fixed logical clause order is what makes the distinction inevitable.Step 6/ 6· Worked examples2 min
Problem
From Sales(region, amount) rows: (East,100),(East,NULL),(West,200),(West,300),(North,50), write a query to show each region's total and count of sales, keeping only regions whose total exceeds 150, ordered by total descending. Then explain why COUNT(amount) and COUNT(*) differ, and why WHERE cannot replace HAVING.
Try it on paper first.
Recall
Keep this
Aggregation questions test whether you truly understand SQL execution order. "WHERE vs HAVING" and "COUNT(*) vs COUNT(col)" are classic interview traps that catch candidates who guess.
COUNT, SUM, AVG, MIN, MAX collapse many rows into one value. COUNT(*) counts all rows including NULLs; COUNT(column) ignores NULLs; COUNT(DISTINCT col) counts unique non-NULL values. Aggregates (except COUNT(*)) skip NULLs.
GROUP BY partitions rows into groups so an aggregate is computed per group. HAVING filters groups after aggregation; WHERE filters rows before grouping.
| Clause | Filters | Aggregates allowed? |
|---|---|---|
| WHERE | rows (pre-grouping) | no |
| HAVING | groups (post-aggregation) | yes |
FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY
This is why you can't use a SELECT alias in WHERE (WHERE runs first) and why aggregate conditions must go in HAVING.
AVG divides by the count of non-NULLs.SELECT must appear in GROUP BY (standard SQL) — a frequent error.HAVING can reference aggregates (e.g. HAVING COUNT(*) > 5); WHERE cannot.WHERE COUNT(*) > 5) — it must be in HAVING."WHERE vs HAVING", "COUNT(*) vs COUNT(col)", "SQL logical execution order", "write a query with GROUP BY + HAVING", and "why can't I use an alias in WHERE?".
Aggregates (COUNT/SUM/AVG/MIN/MAX) collapse rows; COUNT(*) includes NULLs, COUNT(col) skips them. GROUP BY groups; WHERE filters rows pre-grouping, HAVING filters groups post-aggregation. Execution order: FROM→WHERE→GROUP BY→HAVING→SELECT→ORDER BY. Aggregates go in HAVING, not WHERE.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.