Sub topic 7
EXISTS or IN: when each reads better
Assume you know SELECT, WHERE, INNER JOIN, LEFT JOIN, aggregate functions, table aliases, and NULL. This chapter focuses on choosing between EXISTS, IN, and JOIN when one table is related to another.
Start with the question
The clearest SQL often comes from matching the query to the question:
- Use
EXISTSwhen you ask, “Does at least one related row exist?” - Use
INwhen you ask, “Is this value in this set of values?” - Use
JOINwhen you need columns from both tables, or when you want to combine rows.
Suppose customers has one row per customer and orders has many rows per customer. To find customers who have placed at least one order, EXISTS expresses the question directly:
SELECT c.customer_id, c.name
FROM customers c
WHERE EXISTS (
SELECT 1
FROM orders o
WHERE o.customer_id = c.customer_id
);The inner query is correlated because it refers to c.customer_id, a value from the current row of the outer query. For each customer, EXISTS becomes true as soon as the inner query finds one matching order.
When `IN` reads better
IN compares one expression with the values returned by an uncorrelated subquery. For the same result, you can write:
SELECT c.customer_id, c.name
FROM customers c
WHERE c.customer_id IN (
SELECT o.customer_id
FROM orders o
);This reads naturally as “find customers whose ID is in the set of customer IDs found in orders.” The subquery does not refer to the outer query, so it is uncorrelated.
Use IN when the set itself is the important idea. For example, this query finds products belonging to selected categories:
SELECT p.product_id, p.name
FROM products p
WHERE p.category_id IN (
SELECT c.category_id
FROM categories c
WHERE c.department = 'Books'
);Here, the inner query first defines the allowed category IDs. The outer query checks membership in that set.
EXISTS versus JOIN
A join combines matching rows. Because one customer can have many orders, a direct join repeats the customer once for every matching order:
SELECT c.customer_id, c.name
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id;If you need one result row per customer, use DISTINCT:
SELECT DISTINCT c.customer_id, c.name
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id;EXISTS avoids this duplicate-row issue because it tests the relationship without returning order rows. Prefer it when you only need columns from customers.
Use a join when you need related data:
SELECT c.customer_id, c.name, o.order_id, o.total
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id;A database optimizer may transform EXISTS or IN into a semi-join internally. A semi-join returns an outer row once when at least one match exists. You should still choose the form that communicates your intent clearly.
The NULL difference
For positive tests, IN and EXISTS often produce equivalent results when the compared columns contain ordinary non-NULL values. Be more careful with negative tests.
This query can fail unexpectedly if the subquery returns even one NULL:
SELECT c.customer_id, c.name
FROM customers c
WHERE c.customer_id NOT IN (
SELECT o.customer_id
FROM orders o
);SQL uses three-valued logic: a comparison can be true, false, or unknown. If the set contains NULL, a value that is not equal to any known value still cannot be proven different from NULL, so the NOT IN condition becomes unknown.
For “customers with no orders,” use NOT EXISTS:
SELECT c.customer_id, c.name
FROM customers c
WHERE NOT EXISTS (
SELECT 1
FROM orders o
WHERE o.customer_id = c.customer_id
);This checks each customer independently and does not have the same NULL trap.
A practical choice
| Question | Usually clearest |
|---|---|
| Does at least one related row exist? | `EXISTS` |
| Does no related row exist? | `NOT EXISTS` |
| Is a value in a known set? | `IN` |
| Do I need columns from both tables? | `JOIN` |
Exercise
Rewrite this query using EXISTS so that each customer appears at most once: SELECT DISTINCT c.customerid FROM customers c JOIN orders o ON o.customerid = c.customer_id;
Show answer
SELECT c.customerid FROM customers c WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customerid = c.customer_id);