← Back to SQL, page 5

sql-0095 · Hard senior

What do the ANY and ALL operators do with a subquery? What is salary > ALL (empty set)?

x > ANY (subquery) is true if x is greater than at least one value; x > ALL (subquery) is true if x is greater than every value. Over an empty set, ALL is true and ANY is false. A NULL in the set can turn a would-be false into UNKNOWN. PostgreSQL, MySQL and SQL Server support them; SQLite does not, so there you use MIN, MAX or EXISTS.

Full answer

> ALL and > ANY compare a value with every row of a subquery.

sql
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?

sql
SELECT name, salary FROM employees
WHERE salary > ALL (SELECT salary FROM employees WHERE dept_id = 2)
ORDER BY name;
namesalary
Asha120000
Mei95000
Ravi95000

> ALL (62000, 70000) is the same as > 70000. > ANY is the same as > 62000:

sql
SELECT name, salary FROM employees
WHERE salary > ANY (SELECT salary FROM employees WHERE dept_id = 2)
ORDER BY name;
namesalary
Asha120000
Mei95000
Ravi95000
Tom70000

The empty set is the classic trick question. Legal has no employees, so the subquery returns nothing:

sql
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_emptyany_empty
80

> 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:

sql
SELECT name FROM employees WHERE salary > ALL (SELECT salary FROM employees WHERE dept_id = 2);  -- error on purpose
text
Error: near "ALL": syntax error

The usual rewrites use MAX and MIN, but they differ on the empty set, because MAX of nothing is NULL:

sql
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.

sql
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