MySQL EXISTS
EXISTS (subquery) is true when the subquery returns at least one row. It doesn't care what the rows contain, only whether there are any. That makes it the natural way to ask "is there a related row?".
SELECT columns
FROM table1 AS t
WHERE EXISTS (SELECT 1 FROM table2 WHERE table2.col = t.col);A correlated subquery
The subquery inside EXISTS usually refers to the outer row (c.customer_id below). It is a correlated subquery: conceptually, it is evaluated again for each customer.
"Customers who have at least one order that is still processing":
SELECT c.customer_id, c.first_name, c.last_name
FROM customers AS c
WHERE EXISTS (
SELECT 1
FROM orders AS o
WHERE o.customer_id = c.customer_id
AND o.status = 'processing'
)
ORDER BY c.customer_id;+-------------+------------+-----------+
| customer_id | first_name | last_name |
+-------------+------------+-----------+
| 8 | Hana | Sato |
| 12 | Lucia | Garcia |
| 15 | Olivia | Moreau |
+-------------+------------+-----------+
SELECT 1 is a convention: the select list of an EXISTS subquery is ignored, so SELECT * or SELECT o.order_id would do the same.
NOT EXISTS
NOT EXISTS finds rows without a related row. Products that were never ordered:
SELECT p.product_id, p.name
FROM products AS p
WHERE NOT EXISTS (
SELECT 1 FROM order_details AS d WHERE d.product_id = p.product_id
);+------------+----------------------+
| product_id | name |
+------------+----------------------+
| 4 | Sparkling Water 6x1L |
| 8 | Rye Crackers |
+------------+----------------------+
This gives the same answer as the LEFT JOIN ... IS NULL pattern from the joins lesson, and unlike NOT IN it is safe when the subquery could return NULLs. Compare these two ways of asking "which employees manage nobody?":
SELECT COUNT(*) AS not_in_result
FROM employees
WHERE employee_id NOT IN (SELECT manager_id FROM employees);
SELECT first_name
FROM employees AS e
WHERE NOT EXISTS (SELECT 1 FROM employees AS r WHERE r.manager_id = e.employee_id)
ORDER BY employee_id;+---------------+
| not_in_result |
+---------------+
| 0 |
+---------------+
+------------+
| first_name |
+------------+
| Linus |
| Maya |
| Omar |
| Tom |
| Sara |
+------------+
The NOT IN version returns nothing at all, because Grace's NULL manager_id is in the list. NOT EXISTS gives the right five people.
EXISTS versus IN and JOIN
| Want | Typical query |
|---|---|
| rows that have a match | EXISTS, IN (subquery) or JOIN + DISTINCT |
| rows that lack a match | NOT EXISTS or LEFT JOIN ... IS NULL (avoid NOT IN on nullable columns) |
A join returns one row per match, so a customer with three orders appears three times; EXISTS returns each customer once:
SELECT COUNT(*) AS join_rows
FROM customers AS c JOIN orders AS o ON o.customer_id = c.customer_id
WHERE c.country = 'UK';
SELECT COUNT(*) AS exists_rows
FROM customers AS c
WHERE c.country = 'UK'
AND EXISTS (SELECT 1 FROM orders AS o WHERE o.customer_id = c.customer_id);+-----------+
| join_rows |
+-----------+
| 5 |
+-----------+
+-------------+
| exists_rows |
+-------------+
| 2 |
+-------------+
Note: MySQL's optimizer turns
EXISTSandINsubqueries into efficient semi-joins, so pick the form that reads best. The "EXISTS is always faster than IN" rule of thumb dates from much older versions.
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's first column is not NULL
- When the subquery returns 1
Why is NOT EXISTS safer than NOT IN?
- It is faster
- NOT IN returns no rows if the subquery yields a NULL
- NOT IN is deprecated
- NOT EXISTS sorts the result
What does the subquery's select list in EXISTS (SELECT 1 FROM ...) affect?
- The result values
- Nothing; it is ignored
- The number of rows returned
- The sort order