# WHERE Filters

> Keep only the rows you want with WHERE, combine tests with AND, OR and NOT, and read the order they run in.

Subject: SQL | Lesson 7 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/sql/where-filters
Previous: [SELECT & Column Labels](https://codewithanimation.in/sql/select-and-column-labels.md) | Next: [IN, BETWEEN & LIKE](https://codewithanimation.in/sql/in-between-and-like.md)

## WHERE is a test for every row

`WHERE` runs a test on each row of the table. A row stays in the result only when the test is `TRUE`. Rows where the test is `FALSE` or unknown are dropped.

The comparison operators are `=`, `<>` (not equal), `<`, `>`, `<=` and `>=`. You can also write `!=`, which PostgreSQL turns into `<>`.

`AND`, `OR` and `NOT` combine tests. `AND` needs both sides to be true. `OR` needs at least one. `NOT` flips true and false.

## Filter inside the database

A table can hold millions of rows. Asking the database for only the matching ones is much faster than loading everything and filtering in your program.

It also keeps the rule in one place: the query says exactly which rows count.

## Combine tests, and know what runs first

Keep the Red players who scored over 20, plus every Blue player:

```sql
SELECT name, team, points
FROM players
WHERE team = 'Red' AND points > 20
   OR team = 'Blue';
```

SQL runs `NOT` first, then `AND`, then `OR`. So this means `(team = 'Red' AND points > 20) OR team = 'Blue'`. Brackets change the order: `team = 'Red' AND (points > 20 OR team = 'Blue')` keeps only Red players, because a player cannot be Red and Blue at once.

When you are not sure, add brackets. They cost nothing and make the meaning clear to the next reader.

## Common mistake: Forgetting that AND runs before OR

Mixing `AND` and `OR` without brackets is the most common source of wrong results. The query runs fine and returns the wrong rows.

A second trap: `points <> 17` does not keep players whose points are `NULL`. The test is unknown for them, so the row is dropped, even though `NULL` is not 17.

## Tip: Add brackets even when they are not required

Write `(a AND b) OR c` instead of relying on precedence. Anyone reading the query sees your intention at once.

## Try it: Read TRUE, FALSE or UNKNOWN for every row

Pick a `WHERE` condition. The WHERE column shows the test result for each player, and only TRUE rows reach the result.

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

## Check yourself

### Question 1

How does PostgreSQL read `a AND b OR c`?

- A. (a AND b) OR c
- B. a AND (b OR c)
- C. From left to right, one test at a time

Correct answer: A

NOT binds tighter than AND, and AND binds tighter than OR.

- Why not B: That needs brackets. AND runs before OR, not after.
- Why not C: SQL follows precedence rules, not left-to-right order.

### Question 2

Chen's points are NULL. Is Chen kept by `WHERE points <> 17`?

- A. No: the test is unknown, so the row is dropped
- B. Yes: NULL is not 17
- C. The query fails with an error

Correct answer: A

WHERE keeps only rows where the test is TRUE. A comparison with NULL gives unknown.

- Why not B: A comparison with NULL is unknown, not true.
- Why not C: It runs fine. Unknown rows are simply dropped.

### Question 3

Which condition keeps Red players with more than 20 points, plus every Blue player?

- A. team = 'Red' AND points > 20 OR team = 'Blue'
- B. team = 'Red' AND (points > 20 OR team = 'Blue')
- C. team = 'Red' OR team = 'Blue' AND points > 20

Correct answer: A

AND runs before OR, so the unbracketed version already says what we want.

- Why not B: The bracket makes AND team = 'Red' apply to everyone, so Blue players are removed.
- Why not C: AND runs first, so this keeps every Red player and only the Blue players over 20.

## Remember this

- WHERE keeps a row only when its test is TRUE, so FALSE and unknown rows are both dropped.
- NOT runs first, then AND, then OR. Brackets override the order.
- A comparison with NULL is unknown, so `<>` and `=` never keep a NULL row.
- When mixing AND and OR, add brackets so the meaning is obvious.
