SQL TutorialLesson 35Free preview
SQL Having
This lesson is part of SQL · All levels
Summary
Having filtered the grouping results after GROUP BY was run, unlike WHERE which filtered the lines before grouping. HAVING can use aggregate functions, for example, only display categories with more than five product counts. The sequence is WHERE, GROUP BY, HAVING, then ORDER BY.
Key points
- Having filtered the group results from the GROUP BY.
- WHERE's filtering the line before the grouping happens.
- HAVING can use the aggregate function, whereas WHERE can't.
- The order of writing: GROUP BY HAVING ORDER BY.
- When the filtering doesn't use aggregates, WHERE is usually more efficient.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.