MySQL ANY (also written as SOME) and ALL let you compare a column against the entire list of values produced by a subquery. ANY returns true if at least one subquery value satisfies the comparison, while ALL requires every subquery value to satisfy it.
| Operator | Passes When | Empty Subquery Result |
|---|---|---|
= ANY (subquery) |
Matches at least one value (same as IN) |
FALSE |
> ANY (subquery) |
Greater than the smallest subquery value | FALSE |
> ALL (subquery) |
Greater than the largest subquery value | TRUE |
!= ALL (subquery) |
Different from every value (same as NOT IN) |
TRUE |
The first query finds orders in demo_orders that exceed any order from customer 101 (meaning larger than at least one order), and the second finds products cheaper than all Electronics items:
-- Orders larger than at least one order from customer 101
SELECT id, customer_id, total_amount
FROM demo_orders
WHERE total_amount > ANY (
SELECT total_amount
FROM demo_orders
WHERE customer_id = 101
AND total_amount IS NOT NULL
);
-- Products priced strictly lower than EVERY Electronics product
SELECT id, product_name, price
FROM demo_products
WHERE price < ALL (
SELECT price
FROM demo_products
WHERE category = 'Electronics'
AND price IS NOT NULL
);
ANY: Developers sometimes write not-equal ANY expecting row exclusion, but x != ANY (1, 2) is true whenever x differs from either 1 or 2—which matches almost every row in the table! Always use NOT IN or NOT EXISTS to exclude matching rows safely.NULL Values in ALL Subqueries: Even one NULL inside an ALL subquery turns the comparison into UNKNOWN and returns empty results. Add IS NOT NULL inside the inner WHERE clause.ANY and SOME in MySQL?No, SOME is an exact SQL alias for ANY in MySQL and behaves identically in every query.
= ANY different from IN?They are functionally identical for single-column subqueries, though IN is far more common and readable in production SQL.
ALL return rows on an empty subquery while ANY returns none?For an empty set, there is no value to satisfy "at least one" (ANY is false), while "all zero values" holds trivially (ALL is true).
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.