Sub topic 4
Why the SELECT List Inside `EXISTS` Does Not Matter
This chapter assumes you know SELECT, WHERE, table aliases, INNER JOIN, LEFT JOIN, and NULL. It also assumes you can read a subquery and understand that EXISTS tests whether a query returns at least one row.
`EXISTS` tests rows, not values
EXISTS is a Boolean condition. It asks whether its subquery produces at least one row for the current outer row.
For example, this query returns 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
);The subquery is correlated because it refers to c.customer_id, a column from the outer query. For each customer, the database checks whether at least one matching order exists.
The 1 in SELECT 1 is not compared with anything. EXISTS ignores the values in the subquery's result rows. It only needs to know whether a row exists.
These queries have the same meaning:
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
);
SELECT c.customer_id, c.name
FROM customers AS c
WHERE EXISTS (
SELECT o.order_id
FROM orders AS o
WHERE o.customer_id = c.customer_id
);
SELECT c.customer_id, c.name
FROM customers AS c
WHERE EXISTS (
SELECT o.total
FROM orders AS o
WHERE o.customer_id = c.customer_id
);If a customer has one matching order, all three subqueries return a row, so EXISTS is true. If a customer has no matching order, all three return no rows, so EXISTS is false.
Why `SELECT 1` is common
Developers commonly write SELECT 1 because it communicates the intent clearly: the query checks for existence, not for a particular column value.
SELECT * also works inside EXISTS, but it can suggest that the selected data will be used:
SELECT c.customer_id, c.name
FROM customers AS c
WHERE EXISTS (
SELECT *
FROM orders AS o
WHERE o.customer_id = c.customer_id
);The database does not need to return every column merely because the query uses SELECT *. A database optimizer can recognize the existence test and stop after finding a qualifying row. The exact execution depends on the database system and its plan, but the logical meaning remains the same.
| Inner SELECT list | Meaning inside `EXISTS` |
|---|---|
| `SELECT 1` | True if at least one matching row exists |
| `SELECT o.order_id` | True if at least one matching row exists |
| `SELECT *` | True if at least one matching row exists |
Conditions still matter
Although the select list does not matter, the FROM and WHERE clauses matter greatly. They determine which rows the subquery can find.
This query checks whether a customer has an order greater than 100:
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
AND o.total > 100
);The subquery can return many columns or one constant, but it must contain the correct matching conditions. Removing the correlation changes the question:
SELECT c.customer_id, c.name
FROM customers AS c
WHERE EXISTS (
SELECT 1
FROM orders AS o
WHERE o.total > 100
);This version asks whether any order in the entire table is greater than 100. If one such order exists, it returns every customer. It no longer asks whether each individual customer has such an order.
`EXISTS` does not return inner columns
You cannot use EXISTS to place an inner column directly in the outer result:
SELECT c.customer_id, c.name, o.order_id
FROM customers AS c
WHERE EXISTS (
SELECT 1
FROM orders AS o
WHERE o.customer_id = c.customer_id
);The alias o exists only inside the subquery. If you need order columns in the result, use a join, or use a separate scalar subquery that returns one value.
How the test proceeds
Conceptually, the database evaluates the correlation for each outer customer:
The database may transform or optimize this process internally. The logical rule stays the same: the subquery's select list supplies no value to the outer query. Its matching rows supply the existence result.
Exercise
In an EXISTS subquery, what changes when SELECT 1 is replaced with SELECT order_id?
Show answer
The result does not change. EXISTS checks whether at least one qualifying row exists, not which value the select list produces.