WHERE example
SELECT name,
city
FROM customers
WHERE active = 1
AND city = 'London'
ORDER BY id;Each row must meet both conditions because they are joined with AND. An active customer from another city does not get through, and neither does an inactive one from London.
Check the result with small data
We have three customers: two from London, but only one of them is active.
WITH customers(name, city, active) AS (
VALUES ('Amy', 'London', 1),
('Liam', 'London', 0),
('Megan', 'Leeds', 1)
)
SELECT name FROM customers
WHERE city = 'London' AND active = 1;| name |
|---|
| Amy |
WHERE evaluates the condition row by row before returning the columns. Liam lives in London, but he does not meet the active condition.
Check your understanding: What would happen if you removed AND active = 1?
Show answer
Amy and Liam would appear: both live in London and they would no longer need to be active.
How WHERE is used
A condition can use comparisons, ranges, lists, patterns, null checks and combinations with AND, OR and NOT.
What changes from case to case
Group the logic with parentheses
WHERE active = 1
AND (city = 'London' OR city = 'Bristol')AND and OR have different precedence. Parentheses make the intent visible and reduce mistakes when you extend the filter.
NULL needs its own operators
SELECT name
FROM customers
WHERE email IS NULL
ORDER BY id;The result contains David Clark, Peter Hill and Miles Lee. Do not use email = NULL: that comparison never comes out true. Use IS NULL or IS NOT NULL.
Before an UPDATE or DELETE, test the same WHERE with SELECT. Check the affected rows, and only then run the change inside a transaction where the engine and environment allow it.
Common mix-ups with WHERE
SELECT price * stock AS stock_value
FROM products
WHERE stock_value > 500;In portable SQL, WHERE cannot use that output alias. Repeat the expression or calculate the alias in an inner query.
A filter can be true, false or unknown
WHERE keeps the rows for which the condition is true. Null values bring in a third result: unknown. A comparison with NULL is neither true nor false, and rows with an unknown result do not get through the filter.
That is why city <> 'London' does not return customers with no city recorded. If you need them, state that case explicitly: city <> 'London' OR city IS NULL. Combining conditions with AND and OR without parentheses can change the meaning, because AND takes precedence.
Test every filter with a row that meets it, one that does not and one with a missing value, and write down beforehand what you expect from each. The filter works on the row's columns, not on aggregates; to filter groups use HAVING.
Self-contained case to check in SQLite
Liam has no city recorded. Does he appear among the customers who do not live in London?
WITH customers(name, city) AS (
VALUES ('Amy', 'London'), ('Liam', NULL), ('Eva', 'Leeds')
)
SELECT name
FROM customers
WHERE city <> 'London'
ORDER BY name;| name |
|---|
| Eve |
Liam's comparison is unknown, not true, so his row does not get through the filter. Only Eva has a known city other than London.
Check your interpretation: How would you change the condition so that Liam appears too?
Show answer
Add the missing case explicitly: WHERE city <> 'London' OR city IS NULL. Eva and Liam would then appear.
Technical questions: PostgreSQL: SELECT, columns and aliases (opens in a new tab).