TExcelTAD
Home › PostgreSQL › PostgreSQL WHERE

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.

text
SELECT columns
FROM table
WHERE condition;

Comparing with a value

pgsql · try it yourself
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:

pgsql · try it yourself
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:

pgsql · try it yourself
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:

pgsql · try it yourself
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:

pgsql · try it yourself
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.

pgsql · try it yourself
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 SELECT list inside WHERE, because WHERE is evaluated before the select list. Repeat the expression instead (or use a subquery).

Test yourself

Which rows does a WHERE clause keep?

  1. Rows where the condition is true
  2. Rows where it is true or NULL
  3. Rows where it is false
  4. All rows

How do you find customers with no email?

  1. WHERE email = NULL
  2. WHERE email = ''
  3. WHERE email IS NULL
  4. WHERE NOT email

Is WHERE city = 'leeds' going to match a row with city Leeds?

  1. Yes, text comparison ignores case
  2. No, = on text is case-sensitive
  3. Only with an index
  4. Only in psql