Sub topic 3
EXISTS: asking whether any related row is there
Prerequisites and the basic question
This chapter assumes that you know SELECT, WHERE, INNER JOIN, LEFT JOIN, table aliases, aggregate functions, and NULL as a missing value.
A subquery is a query inside another SQL statement. An EXISTS condition asks whether its subquery returns at least one row. It does not ask how many rows match, and it does not return the matching rows themselves.
Suppose customers has one row per customer and orders has one row per order. To find customers who have placed at least one order:
SELECT c.customer_id, c.name
FROM customers AS c
WHERE EXISTS (
SELECT 1
FROM orders AS o
WHERE o.customer_id = c.customer_id
);For each customer row, the inner query searches for an order with the same customer_id. If it finds one row, EXISTS is true for that customer. The database can stop looking after the first match.
Correlation: the link to the outer row
The inner query is correlated because it refers to a column from the outer query:
o.customer_id = c.customer_idThe alias c belongs to the outer query. The alias o belongs to the inner query. Conceptually, SQL evaluates the condition in the context of each outer customer:
- Take one customer from
customers. - Search
ordersfor a matching customer ID. - Keep the customer if at least one order exists.
- Repeat for the next customer.
A database optimizer may transform this process into a more efficient plan. The conceptual model still helps you write and check the condition.
The SELECT list inside EXISTS
The expressions selected by the inner query do not affect the result of EXISTS. The operator only checks whether at least one row exists. For that reason, SELECT 1 is a common convention:
SELECT c.customer_id, c.name
FROM customers AS c
WHERE EXISTS (
SELECT 1
FROM orders AS o
WHERE o.customer_id = c.customer_id
);These forms have the same logical meaning:
WHERE EXISTS (
SELECT o.order_id
FROM orders AS o
WHERE o.customer_id = c.customer_id
)and:
WHERE EXISTS (
SELECT *
FROM orders AS o
WHERE o.customer_id = c.customer_id
)SELECT 1 communicates that the selected value is irrelevant. It does not make the query inherently faster than the other forms.
EXISTS compared with a join
A join returns combined rows. EXISTS returns outer rows based on whether a match is present. This difference matters when one customer has several orders.
This join can repeat a customer:
SELECT c.customer_id, c.name
FROM customers AS c
JOIN orders AS o
ON o.customer_id = c.customer_id;A customer with three orders appears three times. The EXISTS version returns that customer once:
SELECT c.customer_id, c.name
FROM customers AS c
WHERE EXISTS (
SELECT 1
FROM orders AS o
WHERE o.customer_id = c.customer_id
);Use EXISTS when the question is “Does a related row exist?” Use a join when you need columns from the related table or need to produce one result row per match.
| Question | Natural form |
|---|---|
| Which customers have at least one order? | `EXISTS` |
| Which customers have no orders? | `NOT EXISTS` |
| Show each order beside its customer name | `JOIN` |
A common boundary: IN
IN compares one value with a set of values:
SELECT c.customer_id, c.name
FROM customers AS c
WHERE c.customer_id IN (
SELECT o.customer_id
FROM orders AS o
);This can express the same “has an order” question when the relationship is a simple value comparison. EXISTS makes the row relationship explicit and is often easier to extend with additional conditions. The distinction becomes especially important for NOT IN, because NULL values can change its result. That trap is covered later.
Exercise
Why does the EXISTS query return each customer only once when a customer has several orders?
Show answer
EXISTS tests whether at least one matching order exists. It does not join every matching order into the result, so the outer customer row is kept only once.