The difference between WHERE and HAVING in SQL is timing. WHERE filters individual rows before they are grouped. HAVING filters whole groups after GROUP BY and the aggregate functions have run. That is why HAVING can use COUNT(*) or AVG(...), and WHERE cannot.
The examples use the student table of the Northbridge University database in Port Alder (120 students). If you are new to grouping, read [SQL GROUP BY explained](/blog/sql-group-by) first.
In what order does SQL run a query?
You write SELECT first, but the database works in this order:
1. FROM picks the table.
2. WHERE removes rows.
3. GROUP BY forms groups.
4. HAVING removes groups.
5. SELECT computes the output columns.
6. ORDER BY sorts the result.
Aggregates like COUNT(*) do not exist yet at step 2. They exist at step 4. This one fact explains almost every WHERE and HAVING mistake.
What does WHERE do?
WHERE looks at one row at a time. This counts only fourth-year students in each department:
SELECT department_id, COUNT(*) AS students FROM student WHERE year = 4 GROUP BY department_id ORDER BY department_id;
| department_id | students |
|---|---|
| 2 | 4 |
| 3 | 3 |
| 4 | 5 |
| 6 | 3 |
| 7 | 6 |
Students from other years never enter the groups. Departments 1 and 5 have no fourth-year students, so they do not appear.
What does HAVING do?
HAVING looks at one group at a time. This keeps only departments with more than 20 students in total:
SELECT department_id, COUNT(*) AS students FROM student GROUP BY department_id HAVING COUNT(*) > 20 ORDER BY department_id;
| department_id | students |
|---|---|
| 4 | 24 |
| 7 | 23 |
All 120 rows were grouped and counted first. Then the groups with 20 students or fewer were dropped.
Can you use WHERE and HAVING in the same query?
Yes, and it is common. WHERE narrows the rows, then HAVING narrows the groups. This finds departments with more than 5 students who live in Port Alder:
SELECT department_id, COUNT(*) AS students FROM student WHERE city = 'Port Alder' GROUP BY department_id HAVING COUNT(*) > 5 ORDER BY department_id;
| department_id | students |
|---|---|
| 4 | 11 |
| 5 | 10 |
| 7 | 8 |
Read it in execution order: take students from Port Alder, group them by department, keep groups larger than 5.
What happens if you put an aggregate in WHERE?
SQL rejects the query, because the groups do not exist yet at that step:
SELECT department_id, COUNT(*) AS students FROM student WHERE COUNT(*) > 20 GROUP BY department_id;
PostgreSQL answers:
Outputaggregate functions are not allowed in WHEREMove the aggregate condition to HAVING.
Can you use HAVING without GROUP BY?
Yes. Without GROUP BY, the whole table counts as one single group, and HAVING keeps or drops that group:
SELECT COUNT(*) AS students FROM student HAVING COUNT(*) > 100;
This returns one row with 120. Change the condition to > 200 and the result is empty, with no rows at all. This form is rare. Most of the time HAVING comes after GROUP BY.
Does the same condition mean the same thing in both clauses?
No, and this surprises many learners. Compare these two queries on GPA:
SELECT department_id, ROUND(AVG(gpa), 2) AS avg_gpa FROM student WHERE gpa > 3 GROUP BY department_id ORDER BY department_id;
This first drops every student with a GPA of 3 or lower, then averages the rest. Every department shows an average above 3 (from 3.39 to 3.68), because only the high-GPA students are left. It answers a question about the students you kept, which is probably not what you meant.
SELECT department_id, ROUND(AVG(gpa), 2) AS avg_gpa FROM student GROUP BY department_id HAVING AVG(gpa) > 2.6 ORDER BY department_id;
| department_id | avg_gpa |
|---|---|
| 3 | 2.80 |
| 5 | 2.70 |
This averages all students with a GPA in each department, then keeps the departments whose average is above 2.6. Ask yourself: am I filtering people, or departments?
Which one should you use?
| Question | Use |
|---|---|
| Does the condition look at a single row's column value? | WHERE |
Does the condition use COUNT, SUM, AVG, MIN or MAX? | HAVING |
| Do you need both? | WHERE first, then HAVING |
A condition on a plain column, like department_id < 4, also works in HAVING. Still, put it in WHERE. The database can then discard rows early, before it builds the groups, and the query is easier to read.
What to read next
- Practice in your browser: [GROUP BY](/learn/sql/group-by), [HAVING](/learn/sql/having) and [Execution order and errors](/learn/sql/execution-order-errors)
- Related post: [SQL GROUP BY explained](/blog/sql-group-by)
- Handle empty values in your data: [SQL NULL values](/blog/sql-null-values)
- Browse the whole course: [SQL course](/courses/sql)
- #sql
- #where
- #having
- #group by