HAVING

Updated · 8 min read
mediumPostgreSQL 18sqlhavinggroup-bywherequery-orderinterview

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 condition keeps only the groups for which the condition is true.
  • WHERE filters rows before grouping; HAVING filters groups after grouping.
  • HAVING usually tests an aggregate: HAVING sum(amount_usd) > 500.
  • Aggregates aren't allowed in WHERE, and in PostgreSQL a SELECT alias can't be used in HAVING.
  • The logical order of a query is FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT.

The Basics

SQL
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:

SQL▶ Try it
SELECT student_id, sum(amount_usd) AS total
FROM payments
GROUP BY student_id
HAVING sum(amount_usd) > 500
ORDER BY student_id;
Output
 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.

Two groups are made; HAVING keeps the one whose total is over 500

WHERE Versus HAVING

WHEREHAVING
FiltersIndividual rowsWhole groups
RunsBefore GROUP BYAfter GROUP BY
Can use aggregatesNoYes
Typical testpaid_on >= '2025-12-01'sum(amount_usd) > 500

Putting an aggregate in WHERE fails, because at that point there are only rows, no groups:

SQL▶ Try it
SELECT student_id, sum(amount_usd) AS total
FROM payments
WHERE sum(amount_usd) > 500
GROUP BY student_id;
Output
ERROR:  aggregate functions are not allowed in WHERE

And 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:

SQL▶ Try it
SELECT student_id, sum(amount_usd) AS total
FROM payments
GROUP BY student_id
HAVING total > 500;
Output
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?

SQL▶ Try it
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;
Output
 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.

WHERE narrows the rows before grouping; HAVING narrows the groups after

Counting Rows in Each Group

HAVING often counts. Which enrollments were paid in more than one installment?

SQL▶ Try it
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;
Output
 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:

SQL▶ Try it
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;
Output
 student_id | payments |  total
------------+----------+---------
        101 |        4 | 1500.00
(1 row)
StepClauseWhat happens to the payments
1FROM paymentsStart with all 7 payments
2WHERE amount_usd > 220Payment 5 ($200.00) is dropped; 6 rows remain
3GROUP BY student_id2 groups: Srihitha (4 payments), Navika (2)
4HAVING count(*) >= 2Both groups have at least 2, so both stay
5SELECT ...Each group becomes one row: id, count, total; the alias total is born here
6ORDER BY total DESCSrihitha ($1,500.00) first, Navika ($450.00) second; the alias works here
7LIMIT 1Only Srihitha's row is kept
FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT on the real payments

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
SQL▶ Try it
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;
Output
 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.

Quick check: HAVING

3 questions
1Which clause can filter on sum(amount_usd)?
2In which order are these clauses logically processed?
3Why does HAVING total > 500 fail in PostgreSQL when total is a SELECT alias?

Key Takeaways

You want toWrite
Groups with a large totalGROUP BY student_id HAVING sum(amount_usd) > 500
Groups with several rowsHAVING count(*) > 1
Filter rows first, then groupsWHERE paid_on >= '2025-12-01' GROUP BY ... HAVING ...

Was this tutorial helpful?

Lesson 26 of 115