ANY and ALL
ANY and ALL compare one value with every value a subquery returns: ANY is true if the comparison holds for at least one of them, ALL if it holds for every one. "Is this course cheaper than some Java course?" is < ANY. "Is it at least as expensive as every course?" is >= ALL. They're less common than IN and EXISTS, but they appear in exams and in older code, and they explain exactly what IN and NOT IN do. This lesson covers both, their links to IN and NOT IN, and their surprising behavior with an empty subquery.
In One Minute
value op ANY (subquery): true ifvalue op xis true for at least onex.SOMEis the same word.value op ALL (subquery): true ifvalue op xis true for everyx.= ANYmeansIN;<> ALLmeansNOT IN, including itsNULLtrap.- With an empty subquery,
ANYis false andALLis true. maxandminoften say the same thing more plainly:> ALL (...)is> (SELECT max(...)).
ANY
WHERE value operator ANY (subquery)Courses that cost less than at least one Java course:
SELECT title, fee_usd
FROM courses
WHERE fee_usd < ANY (SELECT fee_usd FROM courses WHERE technology = 'Java')
ORDER BY fee_usd; title | fee_usd
-------------------+---------
Git Basics | 250.00
SQL Basics | 400.00
Java Basics | 450.00
JavaScript Basics | 450.00
Java OOP | 500.00
(5 rows)The Java fees are 450.00, 500.00 and 550.00. A course qualifies if its fee is below any one of them, which means below the largest, 550.00. So every course except Java Collections itself is kept. < ANY is the same as < (SELECT max(fee_usd) ...).
ALL
ALL requires the comparison to hold for every value. The most expensive course, as a fee at least as large as every fee:
SELECT title, fee_usd
FROM courses
WHERE fee_usd >= ALL (SELECT fee_usd FROM courses); title | fee_usd
------------------+---------
Java Collections | 550.00
(1 row)Java Collections, at 550.00, is greater than or equal to all six fees, including its own. >= ALL is the same as >= (SELECT max(fee_usd) ...). A course more expensive than every Java course doesn't exist: fee_usd > ALL (SELECT fee_usd FROM courses WHERE technology = 'Java') returns no rows.
ANY and ALL Explain IN and NOT IN
| Written as | Means | Same as |
|---|---|---|
x = ANY (list) | x equals at least one value | x IN (list) |
x <> ALL (list) | x differs from every value | x NOT IN (list) |
x > ANY (list) | x beats at least one | x > min(list) |
x > ALL (list) | x beats every one | x > max(list) |
= ANY is IN:
SELECT first_name
FROM students
WHERE student_id = ANY (SELECT student_id FROM payments)
ORDER BY student_id; first_name
------------
Srihitha
Navika
(2 rows)And <> ALL is NOT IN, including its NULL trap from EXISTS: one NULL in the list makes every comparison unknown, so this returns nothing, even though Navika mentors nobody:
SELECT first_name
FROM students
WHERE student_id <> ALL (SELECT mentor_id FROM students); first_name
------------
(0 rows)When the Subquery Is Empty
With no values to compare against, ANY is false and ALL is true. There are no Python courses:
SELECT count(*) AS cheaper_than_all_python
FROM courses
WHERE fee_usd < ALL (SELECT fee_usd FROM courses WHERE technology = 'Python');
SELECT count(*) AS cheaper_than_any_python
FROM courses
WHERE fee_usd < ANY (SELECT fee_usd FROM courses WHERE technology = 'Python'); cheaper_than_all_python
-------------------------
6
(1 row)
cheaper_than_any_python
-------------------------
0
(1 row)All six courses are "cheaper than every Python course", because there's no Python course to be more expensive than. And none is "cheaper than some Python course", because there isn't one. It's logically correct, but it surprises people, and it's where ALL and max differ: < (SELECT max(...)) over no rows compares with NULL and keeps nothing.
PostgreSQL also accepts arrays, as in WHERE course_id = ANY (ARRAY[1, 4]) (PostgreSQL only), which is handy when a program passes in a list of ids.
Common Mistakes
Reading > ANY as "greater than all". It means greater than at least one, which is greater than the minimum.
<> ALL with NULLs. It returns nothing, like NOT IN.
Forgetting the empty case. ALL over no rows is true, so every row passes.
Interview Corner
What's the difference between ANY and ALL?
ANY is true if the comparison holds for at least one value the subquery returns; ALL is true if it holds for every value.
What is = ANY equivalent to? And <> ALL?
= ANY is IN, and <> ALL is NOT IN, including NOT IN's behavior with NULL.
What does ALL return when the subquery returns no rows?
True. With nothing to compare against, the condition holds "for all" of them; ANY returns false.
Practice
Challenge: List the courses whose fee is greater than the fee of every Beginner course, using ALL. Then think about why the answer might be empty or not.
Show solution
SELECT title, fee_usd
FROM courses
WHERE fee_usd > ALL (SELECT fee_usd FROM courses WHERE level = 'Beginner')
ORDER BY fee_usd; title | fee_usd
------------------+---------
Java OOP | 500.00
Java Collections | 550.00
(2 rows)The Beginner fees are 450.00, 400.00, 450.00 and 250.00, so the bar is 450.00. Java OOP (500.00) and Java Collections (550.00), both Intermediate, are above every one.
Key Takeaways
| You want to | Write |
|---|---|
| Below at least one | fee_usd < ANY (SELECT fee_usd ...) |
| Above every one | fee_usd > ALL (SELECT fee_usd ...) |
| In a list | student_id = ANY (SELECT student_id FROM payments) |
| In an array (PostgreSQL) | course_id = ANY (ARRAY[1, 4]) |
Was this tutorial helpful?