Sub topic 4
Why the SELECT list inside EXISTS does not matter
This chapter assumes you can write a basic EXISTS query, as covered in the previous chapters. You should also know what a correlated subquery is and how table aliases work. Here you will learn why EXISTS (SELECT 1 ...), EXISTS (SELECT * ...) and EXISTS (SELECT NULL ...) all behave the same way. You will also see the one situation where the inner query's shape does change the result.
Setup data
The examples use the same tables as the rest of the tutorial. Run this once to follow along.
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
name VARCHAR(50) NOT NULL
);
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER,
order_date DATE,
total DECIMAL(10,2)
);
INSERT INTO customers (customer_id, name) VALUES (1, 'Ada');
INSERT INTO customers (customer_id, name) VALUES (2, 'Ben');
INSERT INTO customers (customer_id, name) VALUES (3, 'Cy');
INSERT INTO orders (order_id, customer_id, order_date, total) VALUES (10, 1, '2026-01-05', 40.00);
INSERT INTO orders (order_id, customer_id, order_date, total) VALUES (11, 1, '2026-02-10', 60.00);
INSERT INTO orders (order_id, customer_id, order_date, total) VALUES (12, 2, '2026-03-15', 25.00);Ada has two orders, Ben has one, and Cy has none.
What EXISTS actually asks
EXISTS takes a subquery and returns a Boolean. It is TRUE if the subquery produces at least one row. It is FALSE if the subquery produces none. It never returns UNKNOWN.
EXISTS does not read the values in those rows. It does not compare them to anything, and it does not return them to the outer query. The only information it uses is whether a row came back.
So the subquery's SELECT list has nothing to contribute. Each of these queries returns Ada and Ben:
SELECT c.customer_id, c.name
FROM customers c
WHERE EXISTS (
SELECT 1
FROM orders o
WHERE o.customer_id = c.customer_id
);
SELECT c.customer_id, c.name
FROM customers c
WHERE EXISTS (
SELECT *
FROM orders o
WHERE o.customer_id = c.customer_id
);
SELECT c.customer_id, c.name
FROM customers c
WHERE EXISTS (
SELECT NULL
FROM orders o
WHERE o.customer_id = c.customer_id
);The third query surprises many learners. SELECT NULL produces a row containing a NULL, but it is still a row. EXISTS counts rows, not values, so the NULL does not make the test false.
Why the database can ignore the SELECT list
Think about what the engine has to do for each outer row. It looks for one matching row in orders. As soon as it finds one, the answer is known, and it can stop searching. This early exit is why EXISTS is often described as short-circuiting.
If the engine stops at the first match, it has no reason to build the full output row. Most optimizers go further and discard the SELECT list while planning the query. The inner query is reduced to its FROM and WHERE parts: which table to read, which rows match, and which index to use.
Because of this, the choice between SELECT 1 and SELECT * has no effect on speed in mainstream engines. This was not always true in every product in the past, which is where some old advice comes from.
Why people write SELECT 1 anyway
Three common conventions exist. All of them return the same result.
| Style | Why people choose it |
|---|---|
| `SELECT 1` | Signals "I only care that a row exists." It is short and widely used. |
| `SELECT *` | Reads naturally ("is there anything?"). It is common in SQL Server and PostgreSQL code. |
| `SELECT NULL` | Makes it very clear that no value is wanted. It is less common. |
Pick one and use it consistently across your codebase. Consistency helps reviewers more than any technical difference between them.
What the SELECT list does still have to satisfy
"Does not matter" applies to the values, not to validity. The engine still parses and checks the inner query, so a few rules remain.
Column names must exist
If you name a column, it must resolve to something real. This fails at parse time:
SELECT c.customer_id, c.name
FROM customers c
WHERE EXISTS (
SELECT o.no_such_column
FROM orders o
WHERE o.customer_id = c.customer_id
);The database reports an unknown column error, even though the value would never have been used. Writing SELECT 1 avoids this class of mistake entirely, because there are no names to get wrong.
Expressions are usually not evaluated
An expression that would fail for a real row, such as SELECT 1/0, normally runs without an error inside EXISTS. This is because the engine does not compute the SELECT list. Do not build anything on this behavior. Engines are allowed to evaluate the list, and some do in certain plans. Treat it as a reason to write simple lists, not as a feature.
Permissions can still apply
With SELECT *, some systems check that you have access to the table's columns. This is rarely a problem, and it applies equally to SELECT 1, which still needs access to the table. If your engine has column-level privileges, test a restricted account before assuming SELECT * works.
The gotcha: aggregates change the number of rows
Everything above rests on one fact: EXISTS looks at how many rows the inner query returns. The SELECT list can change that count when it contains an aggregate function without GROUP BY.
A query such as SELECT COUNT(*) FROM orders WHERE ... always returns exactly one row, even when nothing matches. In that case the row contains 0. So EXISTS over such a query is always TRUE.
SELECT c.customer_id, c.name
FROM customers c
WHERE EXISTS (
SELECT COUNT(*)
FROM orders o
WHERE o.customer_id = c.customer_id
);This returns Ada, Ben, and Cy. Cy is included even though Cy has no orders, because the inner query returns one row containing 0. The query looks like a "has orders" test, but the aggregate guarantees a row every time.
There are two correct ways to express what was intended:
SELECT c.customer_id, c.name
FROM customers c
WHERE EXISTS (
SELECT 1
FROM orders o
WHERE o.customer_id = c.customer_id
);
SELECT c.customer_id, c.name
FROM customers c
WHERE EXISTS (
SELECT COUNT(*)
FROM orders o
WHERE o.customer_id = c.customer_id
HAVING COUNT(*) > 0
);The first form is simpler and preferred. In the second, HAVING filters the single aggregate row away when the count is zero, so the subquery then returns no rows. It works, but it makes the engine do more than necessary.
The same reasoning applies to a few other clauses that change the number of rows returned:
LIMIT 0(orFETCH FIRST 0 ROWS ONLY) makes the subquery return nothing, soEXISTSis alwaysFALSE.OFFSETcan skip the only matching row, soEXISTSbecomesFALSEeven though a match exists.DISTINCTandORDER BYdo not change whether a row exists. They are wasted work here, and the optimizer usually removes them.
Contrast with IN and scalar subqueries
The SELECT list matters in other subquery forms, which is why the EXISTS rule is easy to forget or overapply.
| Form | Does the SELECT list matter? |
|---|---|
| `EXISTS (subquery)` | No. Only whether a row exists. |
| `x IN (subquery)` | Yes. The subquery must return exactly one column, and its values are compared to `x`. |
| `x > (subquery)` | Yes. The subquery must return one column and at most one row, and its value is used. |
| `SELECT (subquery) AS alias` | Yes. The returned value becomes the output column. |
Compare the scalar example from the SELECT list:
SELECT c.customer_id, c.name,
(SELECT MAX(o.order_date)
FROM orders o
WHERE o.customer_id = c.customer_id) AS last_order_date
FROM customers c;Here MAX(o.order_date) is the whole point. Change it to MAX(o.total) and the output changes. In an EXISTS, that kind of change has no effect on the result.
Practical guidance
- Write
SELECT 1unless your team's style guide says otherwise. It cannot refer to a wrong column and it states your intent. - Do not add
DISTINCTorORDER BYinsideEXISTS. They add no meaning and may add work if the optimizer does not remove them. - Put all the logic in the
WHEREclause of the inner query, including the correlation condition that links to the outer row. Without that condition, the inner query does not depend on the outer row, andEXISTSisTRUEfor every outer row or for none. - Avoid aggregates in the SELECT list of an
EXISTSsubquery. - When reviewing someone else's code, treat any
EXISTSwhose SELECT list has an aggregate or an unusual expression as a likely bug until proven otherwise.
Exercise
A colleague writes WHERE EXISTS (SELECT MAX(o.total) FROM orders o WHERE o.customerid = c.customerid) to find customers who have placed an order. Using the sample data, which customers does it return, and why? Rewrite it so that it is correct.
Show answer
It returns Ada, Ben, and Cy. MAX(...) without GROUP BY always produces exactly one row, and for Cy that row contains NULL. EXISTS only checks that a row came back, so it is TRUE for every customer. The correct version is WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customerid = c.customerid), which returns only Ada and Ben.
Exercise
Do these two queries return the same rows? Explain in one or two sentences. Query A: SELECT c.name FROM customers c WHERE EXISTS (SELECT * FROM orders o WHERE o.customerid = c.customerid); Query B: SELECT c.name FROM customers c WHERE EXISTS (SELECT NULL FROM orders o WHERE o.customerid = c.customerid);
Show answer
Yes, both return Ada and Ben. EXISTS tests only whether the inner query returns a row. A row containing NULL is still a row, and the SELECT list is not used to decide the result.