HAVING vs WHERE
Question
How does HAVING differ from WHERE?
Hint
Use HAVING with aggregate conditions, WHERE with row conditions.
Explanation
WHERE filters individual rows before grouping and aggregation. HAVING filters groups after GROUP BY and aggregation. SELECT dept, COUNT(*) FROM emp GROUP BY dept HAVING COUNT(*) > 5 returns departments with more than 5 employees. You cannot use aggregate functions in WHERE (WHERE COUNT(*) > 5 is invalid). HAVING can use both aggregates and non-aggregate columns. Rule: WHERE for row-level filtering, HAVING for group-level filtering. Both can be used in the same query.
Related Resources
Related Lessons
- GROUP BY
Learn GROUP BY.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.