# WHERE vs HAVING in SQL

Source: https://sqlstreak.com/sql/where-vs-having · Tested on PostgreSQL 18.6 and SQLite 3.54.1; results generated 2026-11-22.


**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 |

## Examples

### 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.

```sql
SELECT species, COUNT(*) AS waiting
FROM pets
WHERE adopted = false
GROUP BY species
ORDER BY species;
```

Result (both engines):

| species | waiting |
| --- | --- |
| bird | 1 |
| cat | 4 |
| dog | 6 |
| rabbit | 1 |

### HAVING: only some groups are kept

Every pet is counted, then `HAVING` keeps the species with more than three of them.

```sql
SELECT species, COUNT(*) AS pets
FROM pets
GROUP BY species
HAVING COUNT(*) > 3
ORDER BY species;
```

Result (both engines):

| species | pets |
| --- | --- |
| cat | 7 |
| dog | 8 |

### Both in one query

Waiting pets only (WHERE), then only species with at least two of them waiting (HAVING).

```sql
SELECT species, COUNT(*) AS waiting
FROM pets
WHERE adopted = false
GROUP BY species
HAVING COUNT(*) >= 2
ORDER BY species;
```

Result (both engines):

| species | waiting |
| --- | --- |
| cat | 4 |
| dog | 6 |

### A SELECT alias in HAVING

SQLite lets HAVING use the alias; PostgreSQL wants the aggregate written out again.

```sql
SELECT species, COUNT(*) AS total
FROM pets
GROUP BY species
HAVING total > 3
ORDER BY species;
```

PostgreSQL: error: column "total" does not exist

SQLite:

| species | total |
| --- | --- |
| cat | 7 |
| dog | 8 |

### An aggregate in WHERE fails

WHERE runs before any group exists, so there is nothing to count yet.

```sql
SELECT species, COUNT(*) AS pets
FROM pets
WHERE COUNT(*) > 3
GROUP BY species;
```

PostgreSQL: error: aggregate functions are not allowed in WHERE

SQLite: error: misuse of aggregate: COUNT()

## 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.
