MySQL ANY and ALL
ANY and ALL compare a value with every value returned by a subquery:
| Expression | True when the comparison holds for … |
|---|---|
x > ANY (subquery) |
at least one value (also spelled SOME) |
x > ALL (subquery) |
every value |
They work with all comparison operators: =, <>, <, <=, >, >=.
ANY
"Products that cost more than at least one product in the Beverages category" means "more than the cheapest beverage":
SELECT unit_price FROM products WHERE category_id = 1 ORDER BY unit_price;
SELECT name, unit_price
FROM products
WHERE category_id = 2
AND unit_price > ANY (SELECT unit_price FROM products WHERE category_id = 1)
ORDER BY unit_price;+------------+
| unit_price |
+------------+
| 3.20 |
| 4.99 |
| 6.75 |
| 24.50 |
+------------+
+----------------+------------+
| name | unit_price |
+----------------+------------+
| Sourdough Loaf | 4.80 |
| Chocolate Cake | 18.00 |
+----------------+------------+
The cheapest beverage costs 3.20, so the bakery products above 3.20 pass.
= ANY is exactly the same as IN:
SELECT first_name FROM customers
WHERE customer_id = ANY (SELECT customer_id FROM orders WHERE status = 'shipped')
ORDER BY first_name;+------------+
| first_name |
+------------+
| Ben |
| Elena |
+------------+
ALL
> ALL means "greater than every value", that is, greater than the maximum. "Employees who earn more than every sales rep":
SELECT first_name, title, salary
FROM employees
WHERE salary > ALL (SELECT salary FROM employees WHERE title = 'Sales Rep')
ORDER BY salary DESC;+------------+-------------------+---------+
| first_name | title | salary |
+------------+-------------------+---------+
| Grace | CEO | 9500.00 |
| Alan | Sales Manager | 6200.00 |
| Ada | Warehouse Manager | 5800.00 |
+------------+-------------------+---------+
<> ALL is the same as NOT IN, including its problem with NULLs.
Edge cases
Two special cases are easy to get wrong:
- If the subquery returns no rows,
x > ALL (...)is true (there's nothing to be smaller than) andx > ANY (...)is false. - If the subquery returns a NULL,
ALLcan no longer be true (the comparison with NULL is unknown), which is the same trap asNOT IN.
SELECT 5 > ALL (SELECT unit_price FROM products WHERE unit_price > 100) AS all_of_empty,
5 > ANY (SELECT unit_price FROM products WHERE unit_price > 100) AS any_of_empty,
5 > ALL (SELECT manager_id FROM employees WHERE manager_id IS NULL OR manager_id = 1) AS all_with_null;+--------------+--------------+---------------+
| all_of_empty | any_of_empty | all_with_null |
+--------------+--------------+---------------+
| 1 | 0 | NULL |
+--------------+--------------+---------------+
MIN/MAX are often clearer
Every ANY/ALL comparison with <, >, <= or >= can be rewritten with MIN or MAX:
| With ANY / ALL | With an aggregate |
|---|---|
x > ANY (SELECT v ...) |
x > (SELECT MIN(v) ...) |
x > ALL (SELECT v ...) |
x > (SELECT MAX(v) ...) |
x < ANY (SELECT v ...) |
x < (SELECT MAX(v) ...) |
x < ALL (SELECT v ...) |
x < (SELECT MIN(v) ...) |
The aggregate versions treat NULLs differently (MIN/MAX skip them) and return NULL instead of true for an empty subquery, so the two forms only agree on non-empty, NULL-free data. Many developers find the MIN/MAX form easier to read.
Test yourself
price > ALL (SELECT price FROM products WHERE category_id = 1) means price is …
- greater than the cheapest beverage
- greater than the most expensive beverage
- equal to some beverage price
- greater than the average beverage price
Which is equivalent to IN (subquery)?
- = ALL
- <> ANY
- = ANY
- > SOME
What is 5 > ALL (subquery) when the subquery returns no rows?
- true (1)
- false (0)
- NULL
- An error