SQL Any, All
SQL ANY and ALL Operators
The ANY and ALL operators are used to compare a value to a set of values returned by a subquery.
Using ANY
The ANY operator returns TRUE if the comparison is true for at least one value in the subquery result.
SELECT column_name FROM table_name WHERE column_name operator ANY ( SELECT column_name FROM another_table );
Example: Price Greater Than Any Order Price
SELECT name, price FROM products WHERE price > ANY ( SELECT price FROM order_items );
This returns products whose price is greater than at least one price in the order_items table.
Using ALL
The ALL operator returns TRUE only if the comparison is true for all values returned by the subquery.
SELECT name, price FROM products WHERE price > ALL ( SELECT price FROM order_items );
This returns products whose price is greater than every price in the order_items table.
Comparison Operators
ANY and ALL are used with comparison operators such as:
- =
- >
- <
- >=
- <=
- <>
ANY vs ALL Summary
- ANY → at least one value matches
- ALL → every value must match
NULL Note
If the subquery returns NULL values, the comparison logic may behave unexpectedly. Always ensure your subquery handles NULL values properly when needed.
Common Mistakes
- Using ANY or ALL without a subquery
- Confusing ANY with IN
- Misunderstanding ALL logic
Next Step
Continue with SQL SELECT INTO to learn how to copy data into a new table.