ANY & ALL

The SQL ANY and ALL Operators

ANY and ALL compare a value with each value returned by a subquery.

OperatorTrue if the comparison is true for...
> ANY (...)at least one value (greater than the minimum)
> ALL (...)every value (greater than the maximum)
= ANY (...)same as IN
<> ALL (...)same as NOT IN

Examples

-- products more expensive than ALL snacks
SELECT ProductName, Price FROM Products
WHERE Price > ALL (SELECT Price FROM Products WHERE Category = 'Snacks');

-- products more expensive than ANY snack
SELECT ProductName, Price FROM Products
WHERE Price > ANY (SELECT Price FROM Products WHERE Category = 'Snacks');

Note: ANY/ALL work in MySQL, PostgreSQL, SQL Server and Oracle, but not SQLite. The equivalents with MAX and MIN work everywhere:

Example

> ALL rewritten with MAX:

SELECT ProductName, Price FROM Products
WHERE Price > (SELECT MAX(Price) FROM Products WHERE Category = 'Snacks');

Result: 3 rows

ProductNamePrice
Basmati Rice 5kg12.5
Olive Oil 1L9.99
Espresso Beans 1kg18