HAVING
HAVING filters groups, the way WHERE filters rows. "Which students have paid more than $500 in total?" can't be answered with WHERE, because no single payment row knows a student's total. The total only exists after GROUP BY has made the groups. HAVING keeps or drops whole groups based on their aggregates. This lesson shows HAVING, how it differs from WHERE, how to use both in one query, and then walks one complete query through the order the database actually runs it in, step by step on real rows.
In One Minute
HAVING conditionkeeps only the groups for which the condition is true.WHEREfilters rows before grouping;HAVINGfilters groups after grouping.HAVINGusually tests an aggregate:HAVING sum(amount_usd) > 500.- Aggregates aren't allowed in
WHERE, and in PostgreSQL aSELECTalias can't be used inHAVING. - The logical order of a query is
FROM,WHERE,GROUP BY,HAVING,SELECT,ORDER BY,LIMIT.
The Basics
SELECT grouping_column, aggregate_function(column)
FROM table_name
WHERE row_condition
GROUP BY grouping_column
HAVING group_condition
ORDER BY ...;Students who have paid more than $500 in total:
SELECT student_id, sum(amount_usd) AS total
FROM payments
GROUP BY student_id
HAVING sum(amount_usd) > 500
ORDER BY student_id; student_id | total
------------+---------
101 | 1700.00
(1 row)GROUP BY made two groups: Srihitha with $1,700.00 and Navika with $450.00. HAVING kept only the group whose total is over $500.
WHERE Versus HAVING
WHERE | HAVING | |
|---|---|---|
| Filters | Individual rows | Whole groups |
| Runs | Before GROUP BY | After GROUP BY |
| Can use aggregates | No | Yes |
| Typical test | paid_on >= '2025-12-01' | sum(amount_usd) > 500 |
Putting an aggregate in WHERE fails, because at that point there are only rows, no groups:
SELECT student_id, sum(amount_usd) AS total
FROM payments
WHERE sum(amount_usd) > 500
GROUP BY student_id;ERROR: aggregate functions are not allowed in WHEREAnd in PostgreSQL, the alias total can't be used in HAVING either, because HAVING runs before the SELECT list is worked out. Repeat the aggregate instead:
SELECT student_id, sum(amount_usd) AS total
FROM payments
GROUP BY student_id
HAVING total > 500;ERROR: column "total" does not exist(MySQL does accept a SELECT alias in HAVING; it's a MySQL only convenience.)
Using WHERE and HAVING Together
WHERE and HAVING work well together: WHERE narrows the rows, then HAVING narrows the groups. Who paid more than $400 from December on?
SELECT student_id, count(*) AS payments, sum(amount_usd) AS total
FROM payments
WHERE paid_on >= '2025-12-01'
GROUP BY student_id
HAVING sum(amount_usd) > 400
ORDER BY student_id; student_id | payments | total
------------+----------+---------
101 | 3 | 1000.00
(1 row)WHERE kept only the four payments from December on. Grouped, Srihitha has three of them ($1,000.00) and Navika one ($225.00). HAVING kept Srihitha.
Counting Rows in Each Group
HAVING often counts. Which enrollments were paid in more than one installment?
SELECT student_id, course_id, count(*) AS installments
FROM payments
GROUP BY student_id, course_id
HAVING count(*) > 1
ORDER BY student_id, course_id; student_id | course_id | installments
------------+-----------+--------------
101 | 2 | 2
103 | 5 | 2
(2 rows)Srihitha's Java OOP and Navika's JavaScript Basics, each paid in two parts.
Under the Hood: The Logical Order of a Query
You write a query in one order, but it's processed in another. This is the logical order, the order that decides what each clause can see. (The database may physically do the work differently, but it always gives the same result as this order.) Here's one complete query, followed through each step on the real payments:
SELECT student_id, count(*) AS payments, sum(amount_usd) AS total
FROM payments
WHERE amount_usd > 220
GROUP BY student_id
HAVING count(*) >= 2
ORDER BY total DESC
LIMIT 1; student_id | payments | total
------------+----------+---------
101 | 4 | 1500.00
(1 row)| Step | Clause | What happens to the payments |
|---|---|---|
| 1 | FROM payments | Start with all 7 payments |
| 2 | WHERE amount_usd > 220 | Payment 5 ($200.00) is dropped; 6 rows remain |
| 3 | GROUP BY student_id | 2 groups: Srihitha (4 payments), Navika (2) |
| 4 | HAVING count(*) >= 2 | Both groups have at least 2, so both stay |
| 5 | SELECT ... | Each group becomes one row: id, count, total; the alias total is born here |
| 6 | ORDER BY total DESC | Srihitha ($1,500.00) first, Navika ($450.00) second; the alias works here |
| 7 | LIMIT 1 | Only Srihitha's row is kept |
This order explains every alias rule you've met. WHERE and HAVING run before SELECT, so they can't see its aliases; ORDER BY runs after, so it can. And HAVING runs after GROUP BY, so it's the first place aggregates can be tested.
Common Mistakes
An aggregate in WHERE. It fails with aggregate functions are not allowed in WHERE. Move the test to HAVING.
Using a SELECT alias in HAVING. PostgreSQL reports column "total" does not exist. Repeat the aggregate.
Putting a row condition in HAVING. HAVING method IS NOT NULL works when you group by method, but WHERE method IS NOT NULL filters earlier and is clearer.
HAVING without GROUP BY. It treats the whole table as one group: SELECT count(*) FROM payments HAVING count(*) > 100 returns no rows at all, not a count of 0.
Interview Corner
What is the difference between WHERE and HAVING?
WHERE filters rows before grouping and can't use aggregates. HAVING filters groups after grouping and usually tests aggregates, such as HAVING sum(amount_usd) > 500.
In what order are the clauses of a SELECT logically processed?
FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, then LIMIT. That's why WHERE and HAVING can't use SELECT aliases in PostgreSQL, while ORDER BY can.
Can you use HAVING without GROUP BY?
Yes. The whole table is treated as one group, so HAVING keeps or drops that single summary row.
Practice
Challenge (module checkpoint): Show the total paid per student, including only students who have paid more than $500. Show student_id, the number of payments and the total, sorted by total, largest first.
Show solution
SELECT student_id, count(*) AS payments, sum(amount_usd) AS total
FROM payments
GROUP BY student_id
HAVING sum(amount_usd) > 500
ORDER BY total DESC; student_id | payments | total
------------+----------+---------
101 | 5 | 1700.00
(1 row)Only Srihitha passes, with five payments totaling $1,700.00. Navika's $450.00 doesn't reach $500, and Advitha has no payments, so she has no group at all.
Key Takeaways
| You want to | Write |
|---|---|
| Groups with a large total | GROUP BY student_id HAVING sum(amount_usd) > 500 |
| Groups with several rows | HAVING count(*) > 1 |
| Filter rows first, then groups | WHERE paid_on >= '2025-12-01' GROUP BY ... HAVING ... |
Was this tutorial helpful?