Careeroza
ANY & ALL
The SQL ANY and ALL Operators
ANY and ALL compare a value with each value returned by a subquery.
| Operator | True 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
| ProductName | Price |
|---|---|
| Basmati Rice 5kg | 12.5 |
| Olive Oil 1L | 9.99 |
| Espresso Beans 1kg | 18 |