Before continuing: This entry is part of the subqueries guide. If you are new to the topic, start with Subquery.
ANY and ALL in SQL: example
In the teaching database, the Audio products cost 54.00 and 69.90. This query asks for products whose price is greater than all those values:
SELECT name, price
FROM products
WHERE price > ALL (
SELECT price
FROM products
WHERE category = 'Audio'
)
ORDER BY price;Since 69.90 is the highest Audio price, in this particular data set the condition matches price > 69.90. The form with ALL still expresses the intent of comparing against whatever set the subquery returns.
How ANY and ALL are used in SQL
The subquery must return a column compatible with the expression on the left. The operator can be, for example, =, <>, <, <=, > or >=.
What changes from case to case
ANY: one true comparison is enough
SELECT name, price
FROM products
WHERE price > ANY (
SELECT price
FROM products
WHERE category = 'Displays'
)
ORDER BY price;The Displays prices are 179 and 249. Exceeding any of them means, with this data and no null values, exceeding 179 at least. The result is Monitor 27 (249) and Pro Laptop (999).
Do not read ANY as “any value” in the everyday sense. Read it as an existence condition: “at least one comparison is true”.
NULL and empty sets change the intuition
Comparisons with NULL give an unknown result. So if no comparison in ANY is true but some are unknown, the result can be NULL instead of false. With ALL, if no comparison is false but some are unknown, the result can also be NULL.
An empty subquery also follows an important rule: ANY comes out false because there is no true comparison; ALL comes out true because no row contradicts the condition.
Rewriting without thinking about NULL or the empty set
For non-null data and a non-empty set, x > ALL (subquery) can look the same as comparing with MAX(...), and x > ANY (...) with MIN(...). That rewrite does not automatically preserve every edge case. If you need a general equivalent, handle null values and the possibility of the subquery returning no rows explicitly.