TExcelTAD
Home › SQL › SQL ANY and ALL

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)
sql
-- 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:

sql · try it yourself
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:

sql · try it yourself
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):

sql · try it yourself
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:

sql · try it yourself
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:

sql · try it yourself
-- "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…

  1. price > (SELECT MIN(price) FROM books)
  2. price > (SELECT MAX(price) FROM books)
  3. price IN (SELECT price FROM books)
  4. price > (SELECT AVG(price) FROM books)

Which is the same as x = ANY (subquery)?

  1. x IN (subquery)
  2. x NOT IN (subquery)
  3. x = ALL (subquery)
  4. EXISTS x

Does SQLite support the ALL operator?

  1. Yes
  2. No — rewrite with MAX/MIN, IN or EXISTS
  3. Only with integers
  4. Only in views