SQL EXISTS
EXISTS tests whether a subquery returns at least one row. It is true as soon as one row is found and false if the subquery is empty.
SELECT columns
FROM table_name
WHERE EXISTS (SELECT 1 FROM other_table WHERE condition);The subquery is usually correlated: it refers to a column of the outer query, so it is evaluated for each outer row.
Customers who have ordered
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
)
ORDER BY c.customer_id
LIMIT 5;customer_id | name
------------+----------------
1 | Corner Cafe
2 | Bright Bytes
3 | Nonna's Kitchen
5 | Le Petit Marche
6 | Code & Coffee
(5 rows)
For each customer c, the subquery looks for an order with that customer's id. What the subquery selects does not matter — SELECT 1, SELECT * and SELECT o.order_id all behave the same, because EXISTS only checks whether a row exists.
NOT EXISTS
Customers who have never ordered:
SELECT c.customer_id, c.name, c.country
FROM customers AS c
WHERE NOT EXISTS (
SELECT 1 FROM orders AS o WHERE o.customer_id = c.customer_id
)
ORDER BY c.customer_id;customer_id | name | country
------------+-----------------+----------
4 | Pixel Studio | Spain
8 | Night Owl Books | USA
10 | Daily Bread | Australia
13 | Priya Kowalski | Germany
19 | Nora Fischer | Austria
23 | Zara Ali | Turkey
24 | Hugo Berg | Sweden
(7 rows)
Unlike NOT IN, NOT EXISTS is not confused by NULLs in the subquery, which makes it the safer choice for "has no …" questions.
More specific conditions
The subquery can test anything. Suppliers that have at least one product out of stock:
SELECT s.name
FROM suppliers AS s
WHERE EXISTS (
SELECT 1 FROM products AS p
WHERE p.supplier_id = s.supplier_id AND p.stock = 0
)
ORDER BY s.name;name
------------------
Green Valley Farms
(1 row)
Employees who have handled a cancelled order:
SELECT e.first_name, e.last_name
FROM employees AS e
WHERE EXISTS (
SELECT 1 FROM orders AS o
WHERE o.employee_id = e.employee_id AND o.status = 'cancelled'
)
ORDER BY e.first_name;first_name | last_name
-----------+----------
Aisha | Bello
Owen | Price
Rosa | Delgado
Tom | Becker
(4 rows)
EXISTS as a value
In SQLite, EXISTS (…) is an expression that yields 1 or 0, so it can also appear in the select list:
SELECT p.name,
EXISTS (SELECT 1 FROM order_items AS oi WHERE oi.product_id = p.product_id) AS ever_sold
FROM products AS p
WHERE p.category_id = 4
ORDER BY p.name;name | ever_sold
----------------+----------
Apples | 1
Avocados | 0
Bananas | 1
Cherry Tomatoes | 1
(4 rows)
EXISTS, IN or JOIN?
| Question | Typical tool |
|---|---|
| Rows that have a match | EXISTS, IN, or JOIN + DISTINCT |
| Rows without a match | NOT EXISTS (or LEFT JOIN … IS NULL) |
| Need columns from the other table | JOIN |
Modern query planners often execute EXISTS and IN the same way. EXISTS avoids the duplicate rows a join can produce, and NOT EXISTS avoids the NULL trap of NOT IN.
Test yourself
When is EXISTS (subquery) true?
- When the subquery returns exactly one row
- When the subquery returns at least one row
- When the subquery returns no NULLs
- When the subquery's first column is true
Why is NOT EXISTS often preferred over NOT IN?
- It is shorter
- It is not affected by NULLs in the subquery
- It returns more columns
- NOT IN is not standard