ANY and ALL

Updated · 7 min read
mediumPostgreSQL 18sqlanyallsomesubqueries

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 if value op x is true for at least one x. SOME is the same word.
  • value op ALL (subquery): true if value op x is true for every x.
  • = ANY means IN; <> ALL means NOT IN, including its NULL trap.
  • With an empty subquery, ANY is false and ALL is true.
  • max and min often say the same thing more plainly: > ALL (...) is > (SELECT max(...)).

ANY

SQL
WHERE value operator ANY (subquery)

Courses that cost less than at least one Java course:

SQL▶ Try it
SELECT title, fee_usd
FROM courses
WHERE fee_usd < ANY (SELECT fee_usd FROM courses WHERE technology = 'Java')
ORDER BY fee_usd;
Output
       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) ...).

Each fee is compared with the three Java fees; below at least one of them is enough

ALL

ALL requires the comparison to hold for every value. The most expensive course, as a fee at least as large as every fee:

SQL▶ Try it
SELECT title, fee_usd
FROM courses
WHERE fee_usd >= ALL (SELECT fee_usd FROM courses);
Output
      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 asMeansSame as
x = ANY (list)x equals at least one valuex IN (list)
x <> ALL (list)x differs from every valuex NOT IN (list)
x > ANY (list)x beats at least onex > min(list)
x > ALL (list)x beats every onex > max(list)

= ANY is IN:

SQL▶ Try it
SELECT first_name
FROM students
WHERE student_id = ANY (SELECT student_id FROM payments)
ORDER BY student_id;
Output
 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:

SQL▶ Try it
SELECT first_name
FROM students
WHERE student_id <> ALL (SELECT mentor_id FROM students);
Output
 first_name
------------
(0 rows)
= ANY keeps Srihitha and Navika; <> ALL against a list with NULL keeps nobody

When the Subquery Is Empty

With no values to compare against, ANY is false and ALL is true. There are no Python courses:

SQL▶ Try it
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');
Output
 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.

With no Python courses, < ALL keeps all 6 courses and < ANY keeps none

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
SQL▶ Try it
SELECT title, fee_usd
FROM courses
WHERE fee_usd > ALL (SELECT fee_usd FROM courses WHERE level = 'Beginner')
ORDER BY fee_usd;
Output
      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.

Quick check: ANY and ALL

3 questions
1The Java fees are 450, 500 and 550. Which fees satisfy fee_usd < ANY (Java fees)?
2Which is equivalent to student_id = ANY (SELECT student_id FROM payments)?
3There are no Python courses. What does fee_usd < ALL (Python fees) return for each course?

Key Takeaways

You want toWrite
Below at least onefee_usd < ANY (SELECT fee_usd ...)
Above every onefee_usd > ALL (SELECT fee_usd ...)
In a liststudent_id = ANY (SELECT student_id FROM payments)
In an array (PostgreSQL)course_id = ANY (ARRAY[1, 4])

Was this tutorial helpful?

Lesson 48 of 115