HAVING — Filtering Grouped Results — Free SQL Tutorial
Learn HAVING — Filtering Grouped Results in SQL with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.
TL;DR: Learn HAVING — Filtering Grouped Results in SQL with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.
Written & reviewed by the Syllab.in Academic Team (CBSE/NCERT subject experts) · Updated
HAVING — Filtering Grouped Results in SQL
HAVING filters the results of GROUP BY. The key difference: WHERE filters individual rows BEFORE grouping, HAVING filters GROUPS AFTER aggregation.
Analogy: WHERE = "only include students with marks > 60". HAVING = "only show cities where the AVERAGE marks > 70". You can't use WHERE for the second condition because you need the group's average to already be calculated.
Rule: WHERE cannot use aggregate functions (AVG, COUNT, SUM). HAVING can. If you want to filter on an aggregate, you MUST use HAVING.
HAVING — Filtering Grouped Results — Syntax
-- WHERE vs HAVING: -- WHERE: filter rows BEFORE grouping SELECT city, AVG(marks) FROM students WHERE marks > 40 -- filters individual students first GROUP BY city; -- HAVING: filter GROUPS after aggregation SELECT city, AVG(marks) FROM students GROUP BY city HAVING AVG(marks) > 70; -- filters cities by their average
Learn HAVING — Filtering Grouped Results step by step with Syllab's free interactive SQL tutorial — runnable code examples, practice exercises and instant AI feedback, all free with no signup. Explore the full SQL course →