Subquery Comparisons: ANY, ALL, Row Value Comparison
IN and EXISTS in Subqueries ask "does it exist?". This chapter uses comparison operators such as =, >, and < with subqueries.
| Form | What the subquery must return | True when |
|---|---|---|
x > (subquery) | 0-1 rows, 1 column | x is greater than that value |
x > ANY (subquery) | Many rows, 1 column | x is greater than at least one value |
x > ALL (subquery) | Many rows, 1 column | x is greater than every value |
(x, y) IN (subquery) | Many rows, 2 columns | An equal pair exists |
Single-row subqueries
A subquery written directly after a comparison operator must return one value. Filtering by the primary key guarantees 1 row.
Customers who joined later than customer 1790
SELECT c.cust_id, c.join_dt
FROM customers c
WHERE c.cust_id <= 2000
AND c.join_dt > (SELECT x.join_dt FROM customers x WHERE x.cust_id = 1790)
ORDER BY c.cust_id
The result is 9 rows, 1791-1799. Join dates repeat in an 1800-day cycle, so customer 1799 is the latest.
If the subquery returns 2 or more rows, an error occurs at run time. The query below fails because the subquery returns 2 rows.
SELECT c.cust_id
FROM customers c
WHERE c.cust_id <= 2000
AND c.join_dt > (SELECT x.join_dt FROM customers x WHERE x.cust_id IN (1790, 1791))
| DB | Error |
|---|---|
| Oracle | ORA-01427: single-row subquery returns more than one row |
| PostgreSQL | more than one row returned by a subquery used as an expression |
| SQL Server | Subquery returned more than 1 value. (Msg 512) |
If the subquery returns 0 rows, it is NULL, not an error. A comparison with NULL is unknown, so the result is 0 rows.
0-row subquery gives a 0-row comparison result
SELECT COUNT(*) AS cnt
FROM customers c
WHERE c.cust_id <= 2000
AND c.join_dt > (SELECT x.join_dt FROM customers x WHERE x.cust_id = 0)
ANY and ALL
When the subquery returns many values, add ANY or ALL after the comparison operator. SOME means the same as ANY.
The example subquery returns two join dates of the GOLD customers (10, 20) among customers 1-20. Join dates increase in customer ID order.
ANY: joined later than some GOLD customer
SELECT c.cust_id, c.join_dt
FROM customers c
WHERE c.cust_id BETWEEN 1 AND 25
AND c.join_dt > ANY (SELECT x.join_dt FROM customers x WHERE x.grade = 'GOLD' AND x.cust_id <= 20)
ORDER BY c.cust_id
ALL: joined later than every GOLD customer
SELECT c.cust_id, c.join_dt
FROM customers c
WHERE c.cust_id BETWEEN 1 AND 25
AND c.join_dt > ALL (SELECT x.join_dt FROM customers x WHERE x.grade = 'GOLD' AND x.cust_id <= 20)
ORDER BY c.cust_id
ANY returns 11-25, later than customer 10's join date; ALL returns 21-25, later than customer 20's join date.
| Expression | Same meaning |
|---|---|
x = ANY (subquery) | x IN (subquery) |
x <> ALL (subquery) | x NOT IN (subquery) |
x <> ANY (subquery) | Differs from at least one value. If there are two or more distinct values, every non-NULL x is true |
Reading <> ANY as "not in the list" is wrong. That meaning is <> ALL.
Empty results and NULL
If the subquery returns 0 rows, ALL is true and ANY is false. "Greater than every value" has no counterexample when there are no values to compare, so it is true.
ALL, ANY, MAX on an empty subquery
SELECT c.cust_id,
CASE WHEN c.cust_id > ALL (SELECT x.cust_id FROM customers x WHERE x.cust_id < 0) THEN 'Y' ELSE 'N' END AS gt_all,
CASE WHEN c.cust_id > ANY (SELECT x.cust_id FROM customers x WHERE x.cust_id < 0) THEN 'Y' ELSE 'N' END AS gt_any,
CASE WHEN c.cust_id > (SELECT MAX(x.cust_id) FROM customers x WHERE x.cust_id < 0) THEN 'Y' ELSE 'N' END AS gt_max
FROM customers c
WHERE c.cust_id BETWEEN 1 AND 3
ORDER BY c.cust_id
Y means the condition is true; N means not true (false or unknown). Only gt_all is Y for all rows. MAX of an empty set is NULL, so > MAX is unknown.
If the subquery result contains a NULL, ALL can never be true. x > ALL (2, 3, NULL) is x > 2 AND x > 3 AND x > NULL, and the last term is unknown. This is the same principle as NOT IN and NULL.
ALL: subquery containing a NULL
SELECT c.cust_id
FROM customers c
WHERE c.cust_id BETWEEN 1 AND 6
AND c.cust_id > ALL (SELECT NULLIF(x.cust_id, 1) FROM customers x WHERE x.cust_id BETWEEN 1 AND 3)
MAX: comparing while skipping NULLs
SELECT c.cust_id
FROM customers c
WHERE c.cust_id BETWEEN 1 AND 6
AND c.cust_id > (SELECT MAX(NULLIF(x.cust_id, 1)) FROM customers x WHERE x.cust_id BETWEEN 1 AND 3)
ORDER BY c.cust_id
The subquery returns NULL, 2, 3. ALL returns 0 rows; MAX returns 4, 5, 6.
ALL and ANY as MAX and MIN
> ALL is "greater than the maximum", and > ANY is "greater than the minimum". Rewriting with an aggregate function makes the subquery return one value, which is easier to read.
| Original | Rewritten | When the results differ |
|---|---|---|
x > ALL (…) | x > (SELECT MAX(…) …) | Subquery returns 0 rows (ALL is true, MAX is unknown); NULLs present (ALL is never true, MAX skips NULLs) |
x < ALL (…) | x < (SELECT MIN(…) …) | Same as above |
x > ANY (…) | x > (SELECT MIN(…) …) | With 0 rows, ANY is false and MIN is unknown |
x < ANY (…) | x < (SELECT MAX(…) …) | Same as above |
The difference on the ANY side (false vs unknown) does not show in WHERE, because both discard the row. If you wrap the condition in NOT (…), false becomes true while unknown stays unknown, so the results differ.
TIP The two expressions are equal only when you know the compared column is NOT NULL and the subquery is not empty. Otherwise, decide what answer you want for an empty result and use the matching form.
Row value comparison: (a, b) IN
To compare two columns as a pair, wrap them in parentheses. The example finds customers whose grade and join date both match those of customers 99-101.
Oracle, PostgreSQL · IN on (grade, join date) pairs
SELECT c.cust_id, c.grade, c.join_dt
FROM customers c
WHERE c.cust_id <= 4000
AND (c.grade, c.join_dt) IN (SELECT x.grade, x.join_dt FROM customers x WHERE x.cust_id BETWEEN 99 AND 101)
ORDER BY c.cust_id
SQL Server does not support row value comparison. Write the same meaning with EXISTS. This form works in all three DBs.
Same meaning with EXISTS
SELECT c.cust_id, c.grade, c.join_dt
FROM customers c
WHERE c.cust_id <= 4000
AND EXISTS (SELECT 1 FROM customers x
WHERE x.cust_id BETWEEN 99 AND 101
AND x.grade = c.grade
AND x.join_dt = c.join_dt)
ORDER BY c.cust_id
The result is 9 rows: 99, 100, 101, and 1899-1901 and 3699-3701, each 1800 apart. Join dates repeat in an 1800-day cycle.
NOTE
(grade, join_dt) IN (SELECT grade, MAX(join_dt) … GROUP BY grade), which finds the row with the maximum value per group, is also a common use of row value comparison. Other methods and tie handling are covered in Group-wise maximum rows.