Full answer
> ALL and > ANY compare a value with every row of a subquery.
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept_id INTEGER REFERENCES departments(id),
manager_id INTEGER REFERENCES employees(id),
salary INTEGER,
hire_date TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
(1,'Asha', 1,NULL,120000,'2019-03-01'),
(2,'Ravi', 1,1, 95000,'2020-07-15'),
(3,'Mei', 1,1, 95000,'2021-01-10'),
(4,'Tom', 2,1, 70000,'2022-05-20'),
(5,'Sara', 2,4, 62000,'2023-02-01'),
(6,'Diego',3,1, 55000,'2021-11-30'),
(7,'Lena', NULL,4, NULL, '2024-06-03'),
(8,'Omar', 3,6, 48000,'2024-09-09');PostgreSQL: who earns more than everyone in Sales?
SELECT name, salary FROM employees
WHERE salary > ALL (SELECT salary FROM employees WHERE dept_id = 2)
ORDER BY name;| name | salary |
|---|---|
| Asha | 120000 |
| Mei | 95000 |
| Ravi | 95000 |
> ALL (62000, 70000) is the same as > 70000. > ANY is the same as > 62000:
SELECT name, salary FROM employees
WHERE salary > ANY (SELECT salary FROM employees WHERE dept_id = 2)
ORDER BY name;| name | salary |
|---|---|
| Asha | 120000 |
| Mei | 95000 |
| Ravi | 95000 |
| Tom | 70000 |
The empty set is the classic trick question. Legal has no employees, so the subquery returns nothing:
SELECT
(SELECT COUNT(*) FROM employees e WHERE e.salary > ALL (SELECT salary FROM employees WHERE dept_id = 4)) AS all_empty,
(SELECT COUNT(*) FROM employees e WHERE e.salary > ANY (SELECT salary FROM employees WHERE dept_id = 4)) AS any_empty;| all_empty | any_empty |
|---|---|
| 8 | 0 |
> ALL over an empty set is true for every row, because there is no Legal salary to fail against. All eight employees are counted, Lena included: her NULL salary is never compared with anything. > ANY over an empty set is false, because no value exists to be greater than.
SQLite does not have ANY or ALL:
SELECT name FROM employees WHERE salary > ALL (SELECT salary FROM employees WHERE dept_id = 2); -- error on purposeError: near "ALL": syntax errorThe usual rewrites use MAX and MIN, but they differ on the empty set, because MAX of nothing is NULL:
SELECT COUNT(*) AS max_version_empty
FROM employees
WHERE salary > (SELECT MAX(salary) FROM employees WHERE dept_id = 4);| max_version_empty |
|---|
| 0 |
The MAX version returns no rows where ALL returned everyone. If empty sets can happen, decide which answer is right and write it explicitly, for example with NOT EXISTS (SELECT 1 FROM ... WHERE s.salary >= e.salary), which matches the ALL behaviour.
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept_id INTEGER REFERENCES departments(id),
manager_id INTEGER REFERENCES employees(id),
salary INTEGER,
hire_date TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
(1,'Asha', 1,NULL,120000,'2019-03-01'),
(2,'Ravi', 1,1, 95000,'2020-07-15'),
(3,'Mei', 1,1, 95000,'2021-01-10'),
(4,'Tom', 2,1, 70000,'2022-05-20'),
(5,'Sara', 2,4, 62000,'2023-02-01'),
(6,'Diego',3,1, 55000,'2021-11-30'),
(7,'Lena', NULL,4, NULL, '2024-06-03'),
(8,'Omar', 3,6, 48000,'2024-09-09');
-- error on purpose
SELECT name FROM employees WHERE salary > ALL (SELECT salary FROM employees WHERE dept_id = 2);
SELECT COUNT(*) AS max_version_empty
FROM employees
WHERE salary > (SELECT MAX(salary) FROM employees WHERE dept_id = 4);Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.
Key points
- > ALL means greater than every value; > ANY means greater than at least one
- ALL over an empty set is TRUE; ANY over an empty set is FALSE
- SQLite has no ANY or ALL
- MAX and MIN rewrites behave differently on empty sets
- NOT EXISTS reproduces ALL semantics exactly
Common mistakes
- Rewriting > ALL as > (SELECT MAX ...) and losing the empty-set behaviour
- Forgetting that a NULL in the set can make ALL return UNKNOWN
Follow-ups
- Rewrite > ALL with NOT EXISTS.
- What does x = ANY (subquery) correspond to?
Tips
- Bring up the empty-set case before you are asked