Before continuing: This entry is part of the subqueries guide. If you are new to the topic, start with Subquery.
EXISTS example
SELECT c.id, c.name
FROM customers AS c
WHERE EXISTS (
SELECT 1
FROM orders AS p
WHERE p.customer_id = c.id
AND p.status = 'shipped'
)
ORDER BY c.id;For each customer, the subquery looks for at least one shipped order with the same customer_id. A customer appears only once, even if several of their orders meet the condition.
What to look for: What matters is that at least one match exists: even if a customer has five shipped orders, the query shows them only once. In this example, SELECT 1 within EXISTS simply signals that we are looking for a row.
Check the result with small data
Amy has two orders and Liam has none.
WITH customers(id, name) AS (VALUES (1,'Amy'),(2,'Liam')),
orders(id, customer_id) AS (VALUES (100,1),(101,1))
SELECT c.name FROM customers c
WHERE EXISTS (SELECT 1 FROM orders p WHERE p.customer_id=c.id);| name |
|---|
| Amy |
EXISTS asks whether there is at least one match. Even though Amy has two orders, she appears only once, because her rows are neither joined nor duplicated.
Check your understanding: Which customer would NOT EXISTS find with the same subquery?
Show answer
Liam: he is the only customer for whom no order with his id exists.
How EXISTS is used
SELECT 1 is a clear convention: it reminds you that the selected value is not used. Any other expression would work too, because EXISTS only checks whether a row appears.
Common mix-ups with EXISTS
Forgetting the link to the outer query
If you remove p.customer_id = c.id, the subquery stops asking about each customer. If there is a single shipped order anywhere in the table, the condition is true for every customer.
Always find the column that links the inner query to the outer row.
Testing for presence without multiplying the outer row
To list customers with at least one paid order, you only need to know whether a match exists. A customer with ten payments still appears once when the outer query has one row per customer. A join with those payments produces ten pairs and answers a different, more detailed question. Choose the form that matches the unit of the result you need.
The correlation is the crucial part. Without a condition linking the order to the outer customer, a single paid order from anyone can make the test true for everyone. Test two customers: one with a match and one without. That test catches a forgotten correlation even when the result on a large database looks plausible.
Whether a row exists does not depend on the value selected inside the subquery; a row with a null value still exists. To test for a missing row you can use NOT EXISTS with the same relationship; its handling of nulls may suit you better than a list with NOT IN. Even so, review unknown keys and the defined relationship: the operator does not invent a match where an equality is not true.
Technical questions: PostgreSQL: Table Expressions and Joins (opens in a new tab).