SQL HAVING
What you will learn
- How
HAVINGfilters groups - The difference between
WHEREandHAVING - The order in which a query is processed
- How to use both in one query
This lesson runs on the sample database you chose in lesson 1.1 (use the Example data picker at the top). Edit any query and press Run. Reset data puts the original rows back.
1. The problem
WHERE removes rows before they are grouped, so it cannot look at the result of an aggregate. To keep only the groups that are big, or have a high total, you need a condition that runs after the grouping: that is HAVING.
Countries with more than 30 customers:
SELECT country, COUNT(*) AS customers FROM customers
GROUP BY country HAVING COUNT(*) > 30 ORDER BY customers DESC, country;
country customers
-------------- ---------
United Kingdom 35
Turkey 32
(2 rows)
Cities with more than 12 customers:
SELECT city, COUNT(*) AS customers FROM customers
GROUP BY city HAVING COUNT(*) > 12 ORDER BY customers DESC, city;
city customers
------------ ---------
Lima 25
Osaka 24
Hanoi 23
Lagos 22
Leeds 22
Manila 22
Lisbon 21
Porto Alegre 21
Pune 18
Toronto 18
Dhaka 17
Austin 15
... (15 rows in total, the first 12 shown)
Years of enrolment with more than 90 students:
SELECT enrolled_year, COUNT(*) AS students FROM students
GROUP BY enrolled_year HAVING COUNT(*) > 90 ORDER BY enrolled_year;
enrolled_year students
------------- --------
2024 102
(1 row)
HAVING comes right after GROUP BY. Its condition uses an aggregate (or a grouped column).
2. WHERE or HAVING?
WHERE |
HAVING |
|
|---|---|---|
| Filters | rows | groups |
| Runs | before grouping | after grouping |
| Can use an aggregate | no | yes |
| Can use a plain column | yes | only a grouped column |
A simple rule: if the condition is about one row (a price, a date, a status), use WHERE. If it is about a group (a count, a total, an average), use HAVING.
3. Both in one query
Use WHERE to choose the rows, then HAVING to choose the groups:
Payments of 2025 or later: which methods have a total above 10,000?
SELECT method, COUNT(*) AS payments, ROUND(SUM(amount), 2) AS total
FROM payments WHERE paid_on >= '2025-01-01'
GROUP BY method HAVING SUM(amount) > 10000 ORDER BY total DESC;
method payments total
-------- -------- ---------
card 1036 102578.42
transfer 389 42688.66
wallet 249 23489.34
cash 199 21100.09
(4 rows)
Open accounts only: which types hold more than 20 million cents?
SELECT account_type, COUNT(*) AS accounts, SUM(balance_cents) AS total_cents
FROM accounts WHERE status = 'open'
GROUP BY account_type HAVING SUM(balance_cents) > 20000000 ORDER BY total_cents DESC;
account_type accounts total_cents
------------ -------- -----------
checking 284 38813900
(1 row)
Students from 2024 onwards: which years have more than 90?
SELECT enrolled_year, COUNT(*) AS students
FROM students WHERE enrolled_year >= 2024
GROUP BY enrolled_year HAVING COUNT(*) > 85 ORDER BY enrolled_year;
enrolled_year students
------------- --------
2024 102
2025 86
2026 89
(3 rows)
4. The order of a query
A query is written in one order but processed in another. Knowing the processing order explains almost every WHERE or HAVING question:
| Step | Clause | What happens |
|---|---|---|
| 1 | FROM |
choose the table |
| 2 | WHERE |
keep the rows that match |
| 3 | GROUP BY |
make the groups |
| 4 | HAVING |
keep the groups that match |
| 5 | SELECT |
calculate the columns and aggregates |
| 6 | ORDER BY |
sort the result |
| 7 | LIMIT |
keep the first rows |
Each step only sees the result of the steps before it. That is why WHERE cannot use an aggregate (the groups do not exist yet at step 2).
5. HAVING and aliases
Whether HAVING may use the alias of an aggregate (HAVING customers > 30) can differ between databases. SQLite allows it. Repeating the aggregate (HAVING COUNT(*) > 30), as the examples above do, works everywhere.
A HAVING condition that uses no aggregate works but belongs in WHERE, where it removes rows earlier.
Worked example: find the repeat buyers
"Which customers placed three or more orders?"
| Step | Query part |
|---|---|
| one row per customer | GROUP BY customer_id |
| how many orders each | COUNT(*) |
| keep three or more | HAVING COUNT(*) >= 3 |
The condition is about the group (the number of orders), so it goes in HAVING, not WHERE. The table is made up.
Common mistakes
- An aggregate in
WHERE. UseHAVING. - A row condition in
HAVING. It works but is slower and less clear; useWHERE. HAVINGwithoutGROUP BY. It then treats the whole table as one group.- Forgetting that
HAVINGruns beforeSELECTaliases in some databases; repeat the aggregate.
Key takeaways
WHEREfilters rows before grouping;HAVINGfilters groups after.- Use
HAVINGfor conditions on counts, totals and averages. - Processing order:
FROM,WHERE,GROUP BY,HAVING,SELECT,ORDER BY,LIMIT. - Put row conditions in
WHEREso fewer rows reach the grouping.
Practice
1. Write a query for the cities with at least 5 customers.
Show answer
SELECT city, COUNT(*) FROM customers GROUP BY city HAVING COUNT(*) >= 5;
2. Why is WHERE COUNT(*) > 5 an error?
Show answer
WHERE runs before the groups exist, so it cannot use an aggregate.
3. Which is correct for "orders placed in 2026, grouped by customer"?
Show answer
WHERE order_date >= '2026-01-01' before GROUP BY: the condition is about each row.
4. What is the first clause processed in a query?
Show answer
FROM.
5. Where does the condition "total above 1000" belong?
Show answer
In HAVING, for example HAVING SUM(amount) > 1000, because it is about a group.
6. Why is a row condition better in WHERE than in HAVING?
Show answer
WHERE removes rows before grouping, so less work is done afterwards.