SQL compared
WHERE vs HAVING in SQL
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
WHERE filters rows before they are grouped; HAVING filters groups after GROUP BY, so only HAVING can test an aggregate like COUNT(*) > 3. Put every condition on plain columns in WHERE, and conditions on totals in HAVING.
Side by side
| WHERE | HAVING | |
|---|---|---|
| Runs | Before grouping, on single rows | After GROUP BY, on whole groups |
| Can test | Columns of each row | Aggregates (COUNT, SUM, AVG…) and grouped columns |
| An aggregate in it | Error | The usual case |
| Effect on the work | Fewer rows to group | Groups are built first, then dropped |
The same data, each way
Every query below was run on PostgreSQL and SQLite when this page was built; the tables are what came back.
WHERE: only some rows are counted
Count the pets still waiting for a home, per species. WHERE drops adopted pets before grouping, so each count covers waiting pets only.
SELECT species, COUNT(*) AS waiting
FROM pets
WHERE adopted = false
GROUP BY species
ORDER BY species;Result (PostgreSQL and SQLite, identical): 4 rows
| species | waiting |
|---|---|
| bird | 1 |
| cat | 4 |
| dog | 6 |
| rabbit | 1 |
Try it: PostgreSQL playground · SQLite playground
HAVING: only some groups are kept
Every pet is counted, then HAVING keeps the species with more than three of them.
SELECT species, COUNT(*) AS pets
FROM pets
GROUP BY species
HAVING COUNT(*) > 3
ORDER BY species;Result (PostgreSQL and SQLite, identical): 2 rows
| species | pets |
|---|---|
| cat | 7 |
| dog | 8 |
Try it: PostgreSQL playground · SQLite playground
Both in one query
Waiting pets only (WHERE), then only species with at least two of them waiting (HAVING).
SELECT species, COUNT(*) AS waiting
FROM pets
WHERE adopted = false
GROUP BY species
HAVING COUNT(*) >= 2
ORDER BY species;Result (PostgreSQL and SQLite, identical): 2 rows
| species | waiting |
|---|---|
| cat | 4 |
| dog | 6 |
Try it: PostgreSQL playground · SQLite playground
A SELECT alias in HAVING
SQLite lets HAVING use the alias; PostgreSQL wants the aggregate written out again.
SELECT species, COUNT(*) AS total
FROM pets
GROUP BY species
HAVING total > 3
ORDER BY species;PostgreSQLERROR: column "total" does not exist
SQLite: 2 rows
| species | total |
|---|---|
| cat | 7 |
| dog | 8 |
Try it: PostgreSQL playground · SQLite playground
An aggregate in WHERE fails
WHERE runs before any group exists, so there is nothing to count yet.
SELECT species, COUNT(*) AS pets
FROM pets
WHERE COUNT(*) > 3
GROUP BY species;PostgreSQLERROR: aggregate functions are not allowed in WHERE
SQLitemisuse of aggregate: COUNT()
Try it: PostgreSQL playground · SQLite playground
When to use which
- WHERE
Any condition on a column's own value: species, status, dates, prices. Even in a grouped query, filtering rows first means less to group.
- HAVING
Conditions on a group's total: how many rows it has, its sum, average, minimum or maximum.
Common mistakes
An aggregate in WHERE (
WHERE COUNT(*) > 3): it fails in both databases. Move it to HAVING.A plain-column filter in HAVING (
HAVING species = 'cat'): it may run, but it filters after grouping. Put it in WHERE.Using a SELECT alias in HAVING (
HAVING total > 3): SQLite accepts it, PostgreSQL doesn't (see “A SELECT alias in HAVING” above). Repeat the aggregate to stay portable.
PostgreSQL and SQLite
The same in both, with one exception shown above: SQLite accepts a SELECT alias in HAVING (HAVING total > 3), PostgreSQL reports that the column doesn't exist.
Official documentation
Go deeper
- Guide: SQL GROUP BY and HAVING
- Practice: SQL GROUP BY and HAVING practice: 8 exercises with answers
- Lesson: HAVING (Pro)
Make it stick in five minutes a day
“HAVING” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.