Filtering with WHERE
Overview
Real tables hold thousands or millions of rows, and most questions are about a few of them: orders still pending, products under $10, customers in India. The WHERE clause is how you say which rows you want. It’s the clause you’ll write more than any other.
How WHERE works
WHERE is followed by a condition — an expression that is true or false for each row. PostgreSQL checks every row and keeps only those where the condition is true.
SELECT name, price, stock
FROM products
WHERE price < 12 AND stock > 0; name | price | stock
-----------------+-------+-------
Assam Breakfast | 9.50 | 80
Chamomile Calm | 8.00 | 12
(2 rows)Comparison operators
| Operator | Meaning |
|---|---|
= |
equal to |
<> or != |
not equal to |
<, <= |
less than, less than or equal |
>, >= |
greater than, greater than or equal |
Note that SQL uses a single = for comparison, not ==. <> is the standard spelling of “not equal”; != works in PostgreSQL and most other databases too.
They work on numbers, text and dates:
SELECT id, status, shipping_fee
FROM orders
WHERE shipping_fee = 0; id | status | shipping_fee
----+-----------+--------------
3 | delivered | 0.00
9 | delivered | 0.00
17 | delivered | 0.00
23 | pending | 0.00
(4 rows)Free shipping: all four of these orders belong to customer 1, a loyalty-scheme member.
SELECT first_name, country
FROM customers
WHERE country <> 'India'; first_name | country
------------+-----------
Ben | UK
Chen | Singapore
Dee | Nigeria
Elena | Spain
Grace | USA
Hugo | France
Jack | USA
(7 rows)Text comparisons are case-sensitive and exact — 'india' is not 'India', and a trailing space makes a different value. For text, < and > compare alphabetically, so WHERE last_name < 'M' finds names in the first half of the alphabet.
AND, OR and NOT
Combine conditions with:
AND— both must be true;OR— at least one must be true;NOT— reverses a condition.
SELECT name, category_id, price
FROM products
WHERE category_id = 2 OR price > 50; name | category_id | price
------------------------+-------------+-------
Darjeeling First Flush | 2 | 18.00
Assam Breakfast | 2 | 9.50
Jasmine Green | 2 | 12.75
Chamomile Calm | 2 | 8.00
Burr Grinder | 3 | 89.00
Electric Kettle | 3 | 59.90
(6 rows)Every tea, plus anything over $50 — whatever its category.
The precedence trap
Suppose you want teas or coffees that cost under $12. This looks right, but isn’t:
SELECT name, category_id, price
FROM products
WHERE category_id = 1 OR category_id = 2 AND price < 12; name | category_id | price
-----------------------+-------------+-------
Ethiopian Yirgacheffe | 1 | 14.50
Colombian Supremo | 1 | 12.00
Decaf House Blend | 1 | 11.25
Assam Breakfast | 2 | 9.50
Chamomile Calm | 2 | 8.00
(5 rows)Ethiopian Yirgacheffe costs 14.50, so why is it here? Because AND is evaluated before OR, just as * is evaluated before + in arithmetic. PostgreSQL read the condition as coffee, OR (tea under $12). Brackets say what you mean:
SELECT name, category_id, price
FROM products
WHERE (category_id = 1 OR category_id = 2) AND price < 12; name | category_id | price
-------------------+-------------+-------
Decaf House Blend | 1 | 11.25
Assam Breakfast | 2 | 9.50
Chamomile Calm | 2 | 8.00
(3 rows)Rule of thumb: whenever a condition mixes
ANDandOR, add brackets — even if you’re sure of the precedence. The next reader may not be.
Ranges with BETWEEN
BETWEEN low AND high is shorthand for >= low AND <= high. Both ends are included:
SELECT name, price
FROM products
WHERE price BETWEEN 12 AND 18; name | price
------------------------+-------
Ethiopian Yirgacheffe | 14.50
Colombian Supremo | 12.00
Darjeeling First Flush | 18.00
Jasmine Green | 12.75
(4 rows)Colombian Supremo at exactly 12.00 and Darjeeling at exactly 18.00 are both included. NOT BETWEEN excludes the range.
Lists with IN
IN (…) tests whether a value equals any item in a list — much tidier than a chain of ORs:
SELECT first_name, city
FROM customers
WHERE city IN ('Pune', 'Leeds', 'Lyon'); first_name | city
------------+-------
Asha | Pune
Ben | Leeds
Farhan | Pune
Hugo | Lyon
(4 rows)NOT IN does the opposite. (It has one dangerous edge case with NULLs, covered in EXISTS and correlated subqueries.)
Filtering on dates and times
Dates are written as text in the ISO format 'YYYY-MM-DD', and PostgreSQL converts them for the comparison:
SELECT first_name, signup_date
FROM customers
WHERE signup_date >= '2026-05-01'; first_name | signup_date
------------+-------------
Hugo | 2026-05-11
Isha | 2026-07-01
Jack | 2026-09-15
(3 rows)ordered_at is a timestamp — a date and a time. That matters at the edges of a range. “Orders in March” is best written as on or after the 1st of March, and before the 1st of April:
SELECT id, ordered_at, status
FROM orders
WHERE ordered_at >= '2026-03-01'
AND ordered_at < '2026-04-01'; id | ordered_at | status
----+---------------------+-----------
7 | 2026-03-02 16:45:00 | delivered
8 | 2026-03-09 11:11:00 | delivered
9 | 2026-03-21 08:30:00 | delivered
(3 rows)Why not BETWEEN '2026-03-01' AND '2026-03-31'? Because a date written without a time means midnight at the start of that day, so anything later on the last day is silently left out. Here’s the problem with a range that ends on a day when an order was placed — “orders from 1 to 28 January”:
SELECT id, ordered_at
FROM orders
WHERE ordered_at BETWEEN '2026-01-01' AND '2026-01-28'; id | ordered_at
----+---------------------
1 | 2026-01-05 09:14:00
2 | 2026-01-12 18:40:00
(2 rows)Order 3 was placed on 28 January at 07:55 — after midnight, so BETWEEN drops it. The half-open version includes it:
SELECT id, ordered_at
FROM orders
WHERE ordered_at >= '2026-01-01'
AND ordered_at < '2026-01-29'; id | ordered_at
----+---------------------
1 | 2026-01-05 09:14:00
2 | 2026-01-12 18:40:00
3 | 2026-01-28 07:55:00
(3 rows)The half-open range >= start AND < next_start is correct for dates, timestamps, and every time zone, and it’s what experienced SQL writers use by habit.
NOT
NOT reverses any condition. This finds every order that is not delivered:
SELECT id, status FROM orders WHERE NOT (status = 'delivered'); id | status
----+-----------
5 | cancelled
15 | cancelled
18 | shipped
20 | shipped
21 | paid
22 | paid
23 | pending
24 | pending
(8 rows)Usually there’s a more direct form — status <> 'delivered', NOT IN, NOT BETWEEN, NOT LIKE, IS NOT NULL — and it reads better.
Conditions are just expressions
A condition is an expression that produces true or false, so you can select it to see exactly what WHERE is testing. This is a great way to debug a filter that doesn’t return what you expect:
SELECT name, price, stock,
price < 12 AS cheap,
stock > 0 AS in_stock,
price < 12 AND stock > 0 AS kept
FROM products
WHERE category_id = 1; name | price | stock | cheap | in_stock | kept
-----------------------+-------+-------+-------+----------+------
Ethiopian Yirgacheffe | 14.50 | 40 | f | t | f
Colombian Supremo | 12.00 | 55 | f | t | f
Decaf House Blend | 11.25 | 0 | t | f | f
(3 rows)psql shows true and false as t and f. Only rows where kept is t would survive the original WHERE.
Tip: when a filter returns too many or too few rows, copy the condition into the select list like this and look at which part evaluates differently from what you expected.
Orders that need attention
The warehouse team starts each day with a list of orders they still have to deal with. Write a query returning the id, customer_id, ordered_at and status of every order whose status is either 'pending' or 'paid' and that was placed on or after 1 November 2026. Use IN for the statuses. You should get orders 22, 23 and 24 — order 21 is paid too, but it's from October.
SELECT id, customer_id, ordered_at, status
FROM orders
WHERE ___;SELECT id, customer_id, ordered_at, status
FROM orders
WHERE status IN ('pending', 'paid')
AND ordered_at >= '2026-11-01';Summary
- WHERE keeps only the rows for which the condition is true; rows where it is false or unknown (NULL) are dropped.
- Comparison operators are =, <> (or !=), <, <=, > and >=; they work on numbers, text and dates.
- Combine conditions with AND, OR and NOT. AND is evaluated before OR, so use brackets whenever you mix them.
- BETWEEN a AND b is inclusive at both ends; IN (…) tests against a list of values.
- Write dates as 'YYYY-MM-DD' text; for timestamps, prefer >= start AND < next_day over BETWEEN.