# HAVING vs WHERE

> Filter rows before grouping with WHERE and filter whole groups after counting with HAVING, and know which test belongs where.

Subject: SQL | Lesson 21 of 44 | 5 min read
Interactive lesson: https://codewithanimation.in/sql/having-vs-where
Previous: [GROUP BY](https://codewithanimation.in/sql/group-by.md) | Next: [Logical Order of a Query](https://codewithanimation.in/sql/logical-order-of-a-query.md)

## Two filters at two moments

`WHERE` filters individual rows **before** they are grouped. `HAVING` filters whole groups **after** the aggregates are computed.

That timing decides what each one can see. `WHERE` can only look at single rows, so it cannot use an aggregate. `HAVING` looks at groups, so it normally tests an aggregate such as `count(*)`.

## Different questions need different filters

"Only count players who have a score" is a question about rows. "Only show teams with at least 3 players" is a question about groups. Using the wrong filter either fails or quietly gives a different answer.

## Teams with at least 3 players

```sql
SELECT team, count(*) AS n
FROM players
GROUP BY team
HAVING count(*) >= 3;
```

Red and Blue have 3 players each, and Green has 2, so the result is Red and Blue. Green was a real group, but `HAVING` removed it after counting.

Now add `WHERE points IS NOT NULL` before `GROUP BY`. `WHERE` removes Chen and Eli first, so every team has only 2 rows left, and `HAVING count(*) >= 3` removes all three teams. The result is empty. Same data, different order of filters, different answer.

## Common mistake: An aggregate in WHERE is an error

`WHERE count(*) >= 3` fails with `aggregate functions are not allowed in WHERE`. At the time `WHERE` runs, the groups do not exist yet, so there is no count to test.

The opposite mistake is legal but wasteful: putting a plain test such as `team = 'Red'` in `HAVING`. It works, but the database groups and counts every row first and only then throws groups away.

## Tip: Plain column test: WHERE. Aggregate test: HAVING

If the test can be done on one row at a time, put it in `WHERE`, so fewer rows reach the grouping step. Use `HAVING` only for tests on aggregates such as `count(*)`, `sum(points)` or `avg(points)`.

## Try it: Combine WHERE and HAVING

Try each WHERE together with each HAVING and read the source table: rows removed by WHERE are greyed out, and groups removed by HAVING say dropped. Then press WHERE count(*) >= 3.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/having-vs-where

## Check yourself

### Question 1

Which query keeps only the teams that have at least 3 players?

- A. GROUP BY team HAVING count(*) >= 3
- B. WHERE count(*) >= 3 GROUP BY team
- C. GROUP BY team WHERE count(*) >= 3

Correct answer: A

The count only exists after grouping, so the test on it belongs in HAVING.

- Why not B: An aggregate is not allowed in WHERE, because no groups exist yet when WHERE runs.
- Why not C: WHERE must come before GROUP BY, and it cannot hold an aggregate anyway.

### Question 2

`WHERE points IS NOT NULL` is added to a query with `HAVING count(*) >= 3`. In the players data, what changes for Red, which had 3 players with one NULL?

- A. Red now has 2 rows to count, so HAVING removes it
- B. Nothing, because HAVING runs first
- C. Red is counted twice

Correct answer: A

WHERE removes the NULL row before the count is made, so Red has 2 rows and fails count(*) >= 3.

- Why not B: WHERE runs before grouping and before HAVING, so the NULL row is already gone when the count is made.
- Why not C: Filtering rows can only reduce the count, never increase it.

### Question 3

You want only the Red team's average. Why is `WHERE team = 'Red'` better than `HAVING team = 'Red'`?

- A. WHERE removes the other rows early, so less grouping and counting is done
- B. HAVING cannot test a plain column
- C. They give different rows

Correct answer: A

The manual says a test with no aggregate is more efficient at the WHERE stage, because rows that fail it are never grouped.

- Why not B: HAVING can use a grouped column. It is just wasteful for a test WHERE could do.
- Why not C: For a test on the grouping column the rows are the same. The difference is the work.

## Remember this

- WHERE filters rows before grouping. HAVING filters groups after the aggregates are computed.
- An aggregate in WHERE is an error: at that time no groups exist yet.
- Put tests on plain columns in WHERE, and tests on aggregates in HAVING.
- A WHERE test can change a group's count, so it can change what HAVING sees.
