Sub topic 9
Semi-joins, anti-joins, and rewriting with JOIN
Assume you know basic SELECT, WHERE, INNER JOIN, LEFT JOIN, table aliases, and NULL. This chapter connects EXISTS and NOT EXISTS to two join patterns: semi-joins and anti-joins.
A semi-join returns the matching outer rows
A semi-join answers this question:
> Which rows in the first table have at least one related row in the second table?
For example, find customers who have placed an order:
SELECT c.customer_id, c.name
FROM customers c
WHERE EXISTS (
SELECT 1
FROM orders o
WHERE o.customer_id = c.customer_id
);The query returns each matching customer once. It does not return order columns, and it does not care whether a customer has one order or twenty orders.
An INNER JOIN can express the same relationship:
SELECT DISTINCT c.customer_id, c.name
FROM customers c
JOIN orders o
ON o.customer_id = c.customer_id;The join first creates one result row for every customer-order pair. If a customer has five orders, that customer appears five times. DISTINCT removes the repeated customer rows.
The logical result is similar, but the intent is different:
| Form | Main question | Duplicate risk |
|---|---|---|
| `EXISTS` | Does at least one related row exist? | No duplicate outer rows |
| `INNER JOIN` | Which row pairs match? | Yes, when several related rows exist |
Use a join when you need columns from both tables or need to process each matching pair. Use EXISTS when the related table only acts as a condition.
An anti-join returns the nonmatching outer rows
An anti-join answers the opposite question:
> Which rows in the first table have no related row in the second table?
Find customers with no orders:
SELECT c.customer_id, c.name
FROM customers c
WHERE NOT EXISTS (
SELECT 1
FROM orders o
WHERE o.customer_id = c.customer_id
);A common join rewrite uses LEFT JOIN and tests for a missing right-side row:
SELECT c.customer_id, c.name
FROM customers c
LEFT JOIN orders o
ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;LEFT JOIN keeps every customer. For a customer without an order, the columns from orders are filled with NULL. The WHERE condition keeps those unmatched customers.
The tested column should be a non-nullable column that identifies an order, such as order_id. Testing o.customer_id IS NULL is unsafe if the database permits an order with a NULL customer ID.
The duplicate-row risk
A join rewrite can silently change the result when you omit DISTINCT:
SELECT c.customer_id, c.name
FROM customers c
JOIN orders o
ON o.customer_id = c.customer_id;This query does not mean “customers with orders” at the result-shape level. It means “customer-order matches.” A customer with multiple orders appears multiple times.
You can remove duplicates with DISTINCT, group the result, or select a key from the outer table and use an aggregate. However, EXISTS states the requirement directly and avoids making duplicate removal part of the query.
Choosing a form
Use these guidelines:
- Choose
EXISTSfor “has at least one” conditions. - Choose
NOT EXISTSfor “has none” conditions. - Choose
INNER JOINwhen you need columns from the related table or one result per match. - Choose
LEFT JOIN ... IS NULLwhen an anti-join fits your team’s SQL style and the tested right-side column cannot be null. - Use
DISTINCTonly when duplicate elimination is part of the intended result.
Both forms may be optimized internally as semi-joins or anti-joins. The SQL spelling expresses the intended result, while the database optimizer chooses an execution strategy.
Exercise
Rewrite this query as an EXISTS query that returns each customer 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);