ANY and ALL compare one value with a whole list of values from a subquery:
ANYis true if the comparison holds for at least one value in the list.ALLis true only if the comparison holds for every value in the list.
SELECT column1FROM table_nameWHERE column1 > ANY (SELECT column2 FROM other_table WHERE condition);The comparison can be any of =, <>, >, >=, < and <=.
The examples use the Crew table from the example database. The accounts department’s day rates are 450, 300 and 420.
The crew members paid more than everyone in accounts:
SELECT Name, DayRateFROM CrewWHERE DayRate > ALL (SELECT DayRate FROM Crew WHERE Department = 'Accounts');| Name | DayRate |
|---|---|
| Maya Okafor | 650 |
| Owen Price | 520 |
| Sam Ellis | 480 |
To pass, a rate must beat 450, 300 and 420 alike, which comes down to beating the highest of them, 450.
The crew members paid more than at least one person in accounts:
SELECT Name, DayRateFROM CrewWHERE DayRate > ANY (SELECT DayRate FROM Crew WHERE Department = 'Accounts');| Name | DayRate |
|---|---|
| Maya Okafor | 650 |
| Tom Hale | 450 |
| Owen Price | 520 |
| Lucy Brennan | 420 |
| Sam Ellis | 480 |
Here a rate only has to beat the lowest, 300. Priya Nair, on 300 herself, is the one person left out.
Simpler ways to say the same
Section titled “Simpler ways to say the same”Each of these has a plainer equivalent, which also works in databases without ANY and ALL, such as SQLite:
| With ANY or ALL | Means the same as |
|---|---|
> ALL (subquery) |
> (SELECT MAX(…) …) |
> ANY (subquery) |
> (SELECT MIN(…) …) |
= ANY (subquery) |
IN (subquery) |
<> ALL (subquery) |
NOT IN (subquery) |