MySQL SQLLesson 37Free preview
MySQL HAVING
This lesson is part of MySQL · All levels
Summary
HAVING is used to filter the results of groupings after GROUP BY is run. If WHERE filters the row before counting, HAVING filters the group based on the results of an aggregate function such as COUNT or SUM. HAVING is the right choice when the filtering conditions involve aggregate functions that are not to be used in WHERE.
Key points
- Having filtered the aggregate results, whereas WHERE filtered the line before the aggregate
- Aggregate functions are prohibited in WHERE but allowed in HAVING
- HAVING is always written after GROUP BY
- Without GROUP BY, HAVING can still be used to filter out a single aggregate result.
- To be efficient, use WHERE to filter the starting line and then HAVING for aggregate results.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.