IN example
SELECT name, city
FROM customers
WHERE city IN ('London', 'Leeds')
ORDER BY city, name;The condition is the same as asking whether city = 'London' or city = 'Leeds', but it states the intent with less repetition.
What to look for: If London and Leeds are in the list, the rows for Manchester are not shown. IN is especially handy when the filter allows several values; it does not remove duplicates from the result.
Check the result with small data
There are three possible statuses and we only want two of them.
WITH orders(id, status) AS (
VALUES (1, 'pending'), (2, 'paid'), (3, 'shipped')
)
SELECT id, status FROM orders
WHERE status IN ('paid', 'shipped') ORDER BY id;| id | status |
|---|---|
| 2 | paid |
| 3 | shipped |
Here IN is the same as comparing the status with «paid» OR with «shipped». It cannot search for part of a string: each listed value must match exactly.
Check your understanding: Which status would you need to add to get all three rows?
Show answer
'pending': WHERE status IN ('pending', 'paid', 'shipped').
How IN is used
The subquery in IN must return a single column when you compare a single value.
What changes from case to case
Expecting true when the set contains NULL
If there is no match but the set contains a NULL, SQL can return unknown instead of true. In a WHERE, that row does not pass the filter.
WHERE id NOT IN (
SELECT customer_id
FROM exclusion_list
)When the subquery column allows NULL, filter those values out explicitly or use NOT EXISTS with a correlated comparison.
IN does not replace every comparison. For ranges, use operators or BETWEEN; for patterns use LIKE; to check for nulls use IS NULL.
IN is the same as several equalities joined with OR
IN checks whether a value matches any item in a list or a subquery. category IN ('Drinks', 'Bakery') is the same as two equalities joined with OR, but shorter and easier to maintain as the list grows.
Nulls behave as they do in any comparison: a row with no category matches no item and does not appear. With NOT IN the risk is greater: if the list or subquery contains a null, the condition is never true and no rows come back. To test for a missing match, prefer NOT EXISTS.
Test one row inside the list, one outside it and one with a missing value. If the list comes from another table, IN with a subquery and EXISTS usually give the same result; the difference shows up with nulls. Duplicate keys do not multiply rows with IN, but they do with a JOIN.
Self-contained case to check in SQLite
Salt has no category recorded; the rest belong to Drinks or Bakery.
WITH products(name, category) AS (
VALUES ('Bread', 'Bakery'), ('Tea', 'Drinks'), ('Coffee', 'Drinks'), ('Salt', NULL)
)
SELECT name
FROM products
WHERE category IN ('Drinks', 'Bakery')
ORDER BY name;| name |
|---|
| Coffee |
| Bread |
| Té |
Salt is left out because its category is null, so it matches no value in the list.
Check your interpretation: What would NOT IN return with the same list?
Show answer
No rows. Bread, Té and Coffee are in the list, and the Salt row does not pass either: NULL NOT IN (...) is unknown, not true.
Technical questions: PostgreSQL: SELECT, columns and aliases (opens in a new tab).