SQL 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 (SOME is a synonym) |
x > ALL (subquery) |
every value |
x = ANY (subquery) |
at least one value — same as x IN (subquery) |
x <> ALL (subquery) |
no value — same as x NOT IN (subquery) |
-- PostgreSQL, MySQL, SQL Server, Oracle
SELECT name, price
FROM products
WHERE price > ALL (SELECT price FROM products WHERE category_id = 8);SQLite has no ANY / ALL
SQLite — the engine on this site — does not support ANY, SOME or ALL. That is not a big loss, because every use can be rewritten with MIN, MAX, IN or EXISTS, and the rewrites work in every database:
| With ANY / ALL | Portable equivalent |
|---|---|
x > ALL (SELECT v …) |
x > (SELECT MAX(v) …) |
x < ALL (SELECT v …) |
x < (SELECT MIN(v) …) |
x > ANY (SELECT v …) |
x > (SELECT MIN(v) …) |
x < ANY (SELECT v …) |
x < (SELECT MAX(v) …) |
x = ANY (SELECT v …) |
x IN (SELECT v …) |
x <> ALL (SELECT v …) |
x NOT IN (SELECT v …) |
Try it — this fails here:
SELECT name FROM products
WHERE price > ALL (SELECT price FROM products WHERE category_id = 8);Error: near "ALL": syntax error
"Greater than ALL" → MAX
Products more expensive than every book:
SELECT name, price
FROM products
WHERE price > (SELECT MAX(price) FROM products WHERE category_id = 8)
ORDER BY price;name | price
----------------------------+------
Chef Knife | 45
Coffee Grinder | 59.9
Mechanical Keyboard | 89
Noise-Cancelling Headphones | 149
(4 rows)
"Greater than ANY" → MIN
Products more expensive than at least one book (that is, more than the cheapest book):
SELECT name, price
FROM products
WHERE price > (SELECT MIN(price) FROM products WHERE category_id = 8)
AND category_id <> 8
ORDER BY price;name | price
----------------------------+------
Power Bank | 29.5
Cast Iron Pan | 38
Chef Knife | 45
Coffee Grinder | 59.9
Mechanical Keyboard | 89
Noise-Cancelling Headphones | 149
(6 rows)
"= ANY" → IN
Products that appear in some order with a quantity of 5:
SELECT product_id, name
FROM products
WHERE product_id IN (SELECT product_id FROM order_items WHERE quantity = 5)
ORDER BY product_id;product_id | name
-----------+--------------------
4 | Sparkling Water
5 | Chai Latte Mix
10 | Whole Milk
15 | Cherry Tomatoes
16 | Bananas
17 | Salted Almonds
19 | Tortilla Chips
22 | Mechanical Keyboard
24 | Power Bank
25 | Chef Knife
26 | Cast Iron Pan
(11 rows)
An edge case: empty subqueries
x > ALL (empty) is true (there is nothing to contradict it), while x > (SELECT MAX(v) … empty) compares with NULL and is unknown. If a subquery could be empty, the NOT EXISTS form matches ALL exactly:
-- "Price greater than all products of category 99" (there are none)
SELECT COUNT(*) AS with_max
FROM products
WHERE price > (SELECT MAX(price) FROM products WHERE category_id = 99);
SELECT COUNT(*) AS with_not_exists
FROM products AS p
WHERE NOT EXISTS (
SELECT 1 FROM products AS q
WHERE q.category_id = 99 AND q.price >= p.price
);with_max
--------
0
(1 row)
with_not_exists
---------------
30
(1 row)
The second form returns all 30 products, as > ALL would.
Test yourself
price > ALL (SELECT price FROM books) is equivalent to…
- price > (SELECT MIN(price) FROM books)
- price > (SELECT MAX(price) FROM books)
- price IN (SELECT price FROM books)
- price > (SELECT AVG(price) FROM books)
Which is the same as x = ANY (subquery)?
- x IN (subquery)
- x NOT IN (subquery)
- x = ALL (subquery)
- EXISTS x
Does SQLite support the ALL operator?
- Yes
- No — rewrite with MAX/MIN, IN or EXISTS
- Only with integers
- Only in views