PostgreSQL WHERE
The WHERE clause filters rows. PostgreSQL evaluates its condition for every row and keeps only the rows where the result is true. Rows where it is false or NULL are dropped.
SELECT columns
FROM table
WHERE condition;Comparing with a value
SELECT title, price
FROM books
WHERE price > 30; title | price
---------------------------+-------
Learning SQL the Calm Way | 39.90
Postgres in Practice | 44.00
Rust for Curious Minds | 42.50
Algorithms on a Napkin | 35.00
(4 rows)
Text values must be quoted with single quotes. Comparisons on text are case-sensitive:
SELECT first_name, city FROM customers WHERE city = 'Leeds';
SELECT first_name, city FROM customers WHERE city = 'leeds'; first_name | city
------------+-------
Ben | Leeds
Olivia | Leeds
(2 rows)
first_name | city
------------+------
(0 rows)
The second query finds nothing: 'leeds' and 'Leeds' are different strings. Use lower(city) = 'leeds' or ILIKE when case should not matter.
Dates and numbers
Dates are written as strings in ISO format ('YYYY-MM-DD') and compared chronologically:
SELECT title, published
FROM books
WHERE published >= '2023-01-01'
ORDER BY published; title | published
------------------------+------------
Clockwork Garden | 2023-02-10
Rust for Curious Minds | 2023-10-08
Postgres in Practice | 2024-04-03
Winter in Sofia | 2024-12-01
The Map Thief | 2025-05-20
(5 rows)
Combining conditions
Use AND, OR and NOT, with parentheses when you mix them:
SELECT title, category_id, price, stock
FROM books
WHERE category_id IN (1, 3)
AND stock > 0
AND NOT (price > 15); title | category_id | price | stock
--------------------+-------------+-------+-------
The Salt Road | 1 | 14.99 | 12
Murder at Low Tide | 3 | 11.99 | 20
The Quiet Witness | 3 | 13.49 | 5
The Map Thief | 3 | 12.99 | 14
Starlight Kitchen | 1 | 13.75 | 10
(5 rows)
Filtering for NULL
= NULL never matches anything. To find missing values, use IS NULL; to exclude them, IS NOT NULL:
SELECT first_name, last_name FROM customers WHERE email = NULL;
SELECT first_name, last_name FROM customers WHERE email IS NULL; first_name | last_name
------------+-----------
(0 rows)
first_name | last_name
------------+-----------
Chloé | Dubois
Gina | Rossi
Mia | Fischer
(3 rows)
WHERE in UPDATE and DELETE
The same condition syntax works in UPDATE and DELETE. A habit that prevents accidents: write the SELECT first, check the rows, then turn it into the change.
SELECT id, title, stock FROM books WHERE stock = 0;
UPDATE books SET stock = 5 WHERE stock = 0; id | title | stock
----+-------------------+-------
2 | Harbor Lights | 0
12 | The Age of Canals | 0
(2 rows)
UPDATE 2
A summary of condition forms
| Condition | Matches |
|---|---|
price = 10 |
exactly 10 |
price <> 10 |
anything except 10 (but not NULL) |
price BETWEEN 10 AND 20 |
10 to 20, both included |
country IN ('UK', 'Ireland') |
one of the listed values |
title LIKE 'The %' |
text starting with "The " |
email IS NULL |
missing value |
tags @> '{space}' |
array contains "space" |
Note: You cannot use a column alias defined in the
SELECTlist insideWHERE, becauseWHEREis evaluated before the select list. Repeat the expression instead (or use a subquery).
Test yourself
Which rows does a WHERE clause keep?
- Rows where the condition is true
- Rows where it is true or NULL
- Rows where it is false
- All rows
How do you find customers with no email?
- WHERE email = NULL
- WHERE email = ''
- WHERE email IS NULL
- WHERE NOT email
Is WHERE city = 'leeds' going to match a row with city Leeds?
- Yes, text comparison ignores case
- No,
=on text is case-sensitive - Only with an index
- Only in psql