Sub topic 5
Comparing Against a Set with ANY and ALL
Prerequisites
This chapter assumes that you can write SELECT and WHERE clauses, use aggregate functions, and work with INNER JOIN and LEFT JOIN. You should also understand that NULL creates an unknown comparison rather than true or false.
A subquery can return several values instead of one. ANY and ALL let the outer query compare one value with that set.
The comparison operators
ANY makes a comparison true when it succeeds for at least one value returned by the subquery. ALL makes it true only when it succeeds for every value returned by the subquery.
| Expression | Meaning |
|---|---|
| `x > ANY (subquery)` | `x` is greater than at least one returned value |
| `x > ALL (subquery)` | `x` is greater than every returned value |
| `x = ANY (subquery)` | `x` equals at least one returned value |
| `x <> ALL (subquery)` | `x` differs from every returned value |
The subquery must return one column. It may return zero, one, or many rows.
Using ANY
Suppose department 10 has salaries of 60,000, 70,000, and 80,000. An employee earning 65,000 is greater than at least one of those salaries, so the following condition is true:
SELECT name, salary
FROM employees
WHERE salary > ANY (
SELECT salary
FROM employees
WHERE department_id = 10
);For a > comparison, > ANY behaves like “greater than the smallest value in the set.” However, writing ANY makes the intended set comparison clear and also works with other operators.
= ANY is commonly equivalent to IN:
SELECT name
FROM customers
WHERE id = ANY (
SELECT customer_id
FROM orders
WHERE total > 100
);For portability, check your database system before using ANY. PostgreSQL and several other systems support this syntax, while some systems provide IN but not the full ANY form.
Using ALL
ALL requires the comparison to succeed against every value returned by the subquery. The next query returns employees whose salary exceeds every salary in department 10:
SELECT name
FROM employees
WHERE salary > ALL (
SELECT salary
FROM employees
WHERE department_id = 10
);For a > comparison, > ALL behaves like “greater than the largest value in the set.” This is stricter than > ANY.
The difference is easier to see with a set of salaries:
| Employee salary | `> ANY (60000, 70000, 80000)` | `> ALL (60000, 70000, 80000)` |
|---|---|---|
| 75,000 | true | false |
The employee is above 60,000 and 70,000, so ANY succeeds. The employee is not above 80,000, so ALL fails.
Empty results and NULL values
An empty subquery has special logical behavior. ANY is false because no value exists that can satisfy the comparison. ALL is true because there is no returned value that disproves the comparison. This result is sometimes called vacuous truth, but you should test it when the subquery might be empty because it can surprise readers.
NULL makes the result less direct. If a subquery returns a NULL and no comparison is true, the overall condition can become unknown. A WHERE clause keeps only true conditions, so unknown rows are excluded.
A useful mental model
Use ANY when one matching comparison is enough. Use ALL when the outer value must exceed, equal, or differ from the entire returned set. Use IN when equality membership is the clearer expression, and use NOT EXISTS when NULL-safe exclusion is required.
Exercise
Write a query that returns employees whose salary is lower than every salary in department 10.
Show answer
SELECT name, salary FROM employees WHERE salary < ALL (SELECT salary FROM employees WHERE department_id = 10);