Sql Having — Free Sql Tutorial

Learn Sql Having in Sql with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.

Sql Having — Free Sql Tutorial

Learn Sql Having in Sql with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.

✓ 100% Free ✓ No Login Needed ✓ NCERT / CBSE Aligned ✓ Download as PDF

TL;DR: Learn Sql Having 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

🤖 Stuck on any question? Ask Syllab's free AI Tutor for a step-by-step explanation — instant, unlimited, no login.

Sql Having 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.

Sql Having — 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 Sql Having 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 →