The PostgreSQL ALL operator compares a value to every value in a set returned by a subquery. It returns true only when the comparison is true for every value in the set. If the subquery returns zero rows, ALL always returns true.
ALL operator syntax
expression operator ALL (subquery)
operatormust be a comparison operator:=,<>,>,>=,<,<=.- The subquery must return exactly one column.
The key semantics by operator:
value > ALL(subquery)— true if value is greater than the maximum of the set.value < ALL(subquery)— true if value is less than the minimum of the set.value = ALL(subquery)— true if value equals every element (all elements are identical).value <> ALL(subquery)— true if value is not equal to any element (equivalent to NOT IN).
Example: employees who earn more than all managers
Using > ALL finds only employees whose salary exceeds every manager's salary — meaning they beat the highest manager salary:
SELECT *
FROM employees
WHERE salary > ALL (
SELECT salary
FROM managers
);
Output:
id | first_name | last_name | salary
----+------------+-----------+----------
1 | Bob | Williams | 75000.00
(1 row)
Bob's salary of 75,000 exceeds the highest manager salary of 60,000, so he is the only match.
Example: employees who earn less than all managers
Using < ALL finds employees whose salary is below every manager's salary — meaning they earn less than the lowest manager salary (55,000):
SELECT *
FROM employees
WHERE salary < ALL (
SELECT salary
FROM managers
)
ORDER BY salary DESC;
Output:
id | first_name | last_name | salary
----+------------+-----------+----------
7 | Harry | Taylor | 53000.00
5 | Frank | Miller | 52000.00
3 | David | Jones | 50000.00
6 | Grace | Wilson | 49000.00
4 | Emma | Brown | 48000.00
8 | Ivy | Moore | 47000.00
10 | Kate | Hill | 44000.00
12 | Mia | Parker | 42000.00
(8 rows)
All 8 employees earn less than 55,000, which is the minimum manager salary.
Tips for using ALL correctly
<> ALLis equivalent toNOT IN— it returns true if the value does not match any element in the set. This is a useful and readable alternative.- Be cautious with
NULLin the subquery result. If any value in the set isNULL,<> ALLevaluates toNULL(unknown) rather than true — the same pitfall asNOT INwith nulls. - ALL with an empty subquery always returns true. If your subquery might return no rows, consider whether that default behavior is correct for your use case.
- For simple "greater than maximum" or "less than minimum" checks, using
> (SELECT MAX(...))or< (SELECT MIN(...))can be more readable and produces equivalent results.
Reference: PostgreSQL documentation — Subquery Expressions (ALL).