Ira SQL ProIra SQL Pro

ANY and ALL Operators

intermediatesubqueries
2 min read

What are ANY and ALL?

ANY and ALL compare a single value against a set of values (from a subquery or array).

  • ANY (or SOME): Returns true if the comparison is true for at least one value in the set.
  • ALL: Returns true only if the comparison is true for every value in the set.

Syntax

SELECT * FROM table_name
WHERE column > ANY (SELECT column FROM other_table);

SELECT * FROM table_name
WHERE column > ALL (SELECT column FROM other_table);

When to Use

  • Comparing a value against multiple possibilities (ANY as an alternative to IN)
  • Ensuring a value is greater/less than all values in a set
  • Working with array comparisons

Key Points

  1. ANY = INcol = ANY(subquery) is equivalent to col IN (subquery).
  2. ALL with >col > ALL(subquery) means col is greater than the maximum of the set.
  3. ANY with >col > ANY(subquery) means col is greater than the minimum of the set.
  4. Empty Set — ALL returns TRUE for empty sets. ANY returns FALSE for empty sets.
  5. Arrays — Works with arrays too: col = ANY(ARRAY[1,2,3]).

Guided Practice

Solve the challenge below. Use hints when stuck and check your answer for instant feedback.

Practice challengeGuided learning mode

ANY and ALL Operators Challenge

Write a query that find students older than at least one grade-B student.

Expected result

Students older than the youngest grade-B student.

Hidden checks

  • Returned rows and values
  • Output columns and result shape
  • Final database state after the query runs

Lesson guidance

What are ANY and ALL?

Initializing database...Each run starts from fresh sample data.

More Examples

Price greater than all products in a category

Find products more expensive than every Electronics item.

Initializing database...Each run starts from fresh sample data.

ANY with an array

Find students with specific IDs using ANY and an array.

Initializing database...Each run starts from fresh sample data.

Frequently Asked Questions

Is ANY the same as IN?
= ANY(subquery) is equivalent to IN (subquery). But ANY also works with other operators like >, <, !=.
What does ALL return for an empty set?
ALL returns TRUE when the subquery is empty, because the condition is vacuously true for all (zero) elements.

Related Topics