filtering with WHERE
comparisons, AND, OR, NOT, BETWEEN and IN. and the missing parentheses around an OR.
- address
- everything.basics.filtering_rows
- part
- part 2, one table
- chapter
- chapter 2 of 12
- reading time
- 7 min
WHERE asks every row the same question, one row at a time, and keeps the rows where the answer is true. comparisons, AND, OR, NOT, BETWEEN and IN are how you write the question.
AND binds tighter than OR, so a OR b AND c means a OR (b AND c). put parentheses around every OR you mix with an AND.
the picture
you know how WHERE feels like searching? it isn't, really. postgres doesn't go looking for the matching rows. it takes every row FROM hands over, asks it your question, and keeps the ones that say yes.
think of a bouncer with a checklist. one person at a time walks up, the bouncer ticks through the list for that person alone, and it's in or out. the bouncer never compares two people, never remembers the last one, and never lets in a "maybe".
- 1SELECT code, title
- 2FROM courses
- 3WHERE credits = 4 AND dept <> 'CS'
- 4ORDER BY code;
| codetext | depttext | creditsint2 | titletext |
|---|---|---|---|
| MATH101 | MATH | 4 | Calculus I |
| PHYS101 | PHYS | 4 | Mechanics |
| PHYS201 | PHYS | 4 | Electromagnetism |
| CS101 | CS | 4 | Intro to Programming |
| CS201 | CS | 4 | Data Structures |
| CS301 | CS | 4 | Databases |
| CS350 | CS | 4 | Operating Systems |
| CS401 | CS | 4 | Database Internals |
| CS499 | GEN | 6 | Senior Thesis |
| HIST101 | HIST | 3 | History of Computing |
| MATH201 | MATH | 3 | Discrete Math |
| MATH301 | MATH | 3 | Linear Algebra |
FROM hands over all 12 courses, drawn with the three that will survive on top, so you can watch the other 9 go.
the query that produced this table
the plan says the same thing in its own words: a scan that reads every row and a filter that throws most of them away.
EXPLAIN (ANALYZE, COSTS OFF, BUFFERS OFF, TIMING OFF, SUMMARY OFF)
SELECT code, title FROM courses WHERE credits = 4 AND dept <> 'CS';Rows Removed by Filter: 9, the same 9 the stepper struck. on a big table, an index lets postgres skip the rows that can't match instead of asking them all, but the answer has to come out the same: WHERE's meaning is "every row, asked one by one", however it's carried out.
comparisons
the six comparison operators work on anything that can be ordered: numbers, text, dates, timestamps.
| operator | means |
|---|---|
= | equal |
<> or != | not equal (the same operator, two spellings) |
< <= > >= | less, at most, greater, at least |
each one gives back a boolean, and WHERE wants exactly that. mysql and sqlite let WHERE 1 mean "every row", because to them a number can be true. postgres won't guess:
SELECT name FROM students WHERE 1;WHERE true is the postgres spelling, if you ever need it. and comparing text compares it exactly, capitals included, in the byte order of the C collation this page runs: 'Zoe' < 'a' is true here. LIKE, ILIKE and regular expressions is the chapter about matching text more loosely.
AND, OR and NOT
AND needs both sides true, OR needs one, NOT flips. when you mix them, they don't apply left to right: NOT goes first, then AND, then OR, just like * before + in arithmetic.
SELECT code, dept, credits
FROM courses
WHERE dept = 'CS' OR dept = 'MATH' AND credits = 3;here's how postgres reads it, and how you probably meant it:
- ├── dept = 'CS'
- └──
- ├── dept = 'MATH'
- └── credits = 3
- ├──
- │ ├── dept = 'CS'
- │ └── dept = 'MATH'
- └── credits = 3
dept = 'MATH' and credits = 3 together first, and the OR is on top.and what each one returns:
- 1SELECT code, dept, credits
- 2FROM courses
- 3WHERE dept = 'CS' OR dept = 'MATH' AND credits = 3
- 4ORDER BY code;
| codetext | depttext | creditsint2 |
|---|---|---|
| CS101 | CS | 4 |
| CS201 | CS | 4 |
| CS301 | CS | 4 |
| CS350 | CS | 4 |
| CS401 | CS | 4 |
| MATH201 | MATH | 3 |
| MATH301 | MATH | 3 |
without parentheses, AND goes first: every CS course, plus the MATH courses with 3 credits. seven rows.
no error, no warning, and the wrong query returns plausible rows. that's what makes it dangerous. the habit that saves you: every time an OR sits next to an AND, put parentheses around the OR, even when you're sure.
BETWEEN and IN
x BETWEEN a AND b is shorthand for x >= a AND x <= b. both ends are included:
SELECT name, price
FROM products
WHERE price BETWEEN 19.99 AND 24.00
ORDER BY price, id;| nametext | pricenumeric |
|---|---|
| left-handed teapot | 19.99 |
| pro chew toy | 20.66 |
| linen yo-yo | 21.03 |
| smart cutting board | 21.48 |
| spare grater | 21.55 |
| heavy desk lamp | 21.74 |
| classic monitor stand | 22.35 |
| small binder | 22.81 |
| bamboo stapler | 23.74 |
| classic soap dish | 23.90 |
| wooden train | 24.00 |
the teapot at exactly 19.99 and the wooden train at exactly 24.00 both make it. NOT BETWEEN is everything outside, ends excluded.
x IN (a, b, c) is shorthand for x = a OR x = b OR x = c, and reads a lot better:
SELECT code, dept, credits
FROM courses
WHERE dept IN ('MATH', 'PHYS')
ORDER BY code;| codetext | depttext | creditsint2 |
|---|---|---|
| MATH101 | MATH | 4 |
| MATH201 | MATH | 3 |
| MATH301 | MATH | 3 |
| PHYS101 | PHYS | 4 |
| PHYS201 | PHYS | 4 |
its opposite, NOT IN, has one nasty edge: a NULL in the list makes it return no rows at all. NULL and three-valued logic shows why, and the NOT IN trap shows the version that bites in real code.
BETWEEN and the last day of the month
BETWEEN's inclusive end is a trap with timestamps. '2026-01-31' compared with a timestamp means midnight at the start of the 31st, so "BETWEEN january 1st and january 31st" stops before the 31st has begun:
- SELECT count(*) AS orders
- FROM orders
- WHERE ordered_at BETWEEN '2026-01-01' AND '2026-01-31';
BETWEEN '2026-01-01' AND '2026-01-31' means up to and including 2026-01-31 00:00:00. every order placed later that day is out, and nothing tells you.
831 orders one way, 859 the other: a whole day of orders gone from a monthly report, with nothing to tell you. write time ranges half-open, >= the start and < the next start, and the boundaries take care of themselves. the time zone matters too: these midnights are UTC, the zone this page runs in (timestamptz).
keylines
- WHERE asks each row the same question on its own, and keeps only the rows that say true.
- comparisons give booleans. postgres won't treat 1 as true.
- NOT, then AND, then OR. put parentheses around an OR that sits next to an AND.
- BETWEEN includes both ends. IN is a row of ORs; NOT IN breaks on a NULL.
- for time ranges, use >= start AND < next start, never BETWEEN.