sqlstreak

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

WHEREHAVING
RunsBefore grouping, on single rowsAfter GROUP BY, on whole groups
Can testColumns of each rowAggregates (COUNT, SUM, AVG…) and grouped columns
An aggregate in itErrorThe usual case
Effect on the workFewer rows to groupGroups 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

specieswaiting
bird1
cat4
dog6
rabbit1

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

speciespets
cat7
dog8

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

specieswaiting
cat4
dog6

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

speciestotal
cat7
dog8

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

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.

Start free