← SQL Deep Dive
2 / 25
Lesson 2 — HAVING vs WHERE
WHERE vs HAVING in one query
WHERE removes rows before grouping — it cannot see aggregates. HAVING removes groups after grouping — it can. Using both in one query is common: first drop the rows you never want, then keep only the groups that pass a threshold.
SELECT team, SUM(points) FROM games
WHERE season = 2026
GROUP BY team HAVING SUM(points) > 10
Why it matters: putting a condition in the wrong place either gives an error (aggregate in WHERE) or quietly wrong numbers.
Want it another way?
Task
Count only finished tasks (status 'done') and show only people with at least 5 such hours. Expected: only Ann with 7.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
Test your knowledge
A quick 3-question quiz on this lesson — instant feedback.