← Back to Chapters

MySQL — ANY & ALL

? MySQL — ANY & ALL

? Quick Overview

The ANY and ALL operators are used with comparison operators to filter results based on a subquery. They let you compare a value to a set of values returned by a subquery: ANY checks if the condition holds for at least one value, while ALL requires the condition to hold for every value.

? Key Concepts

  • ANY — True if the comparison is true for any single value from the subquery.
  • ALL — True only if the comparison is true for all values returned by the subquery.
  • Both are typically used with =, >, <, >=, <=, etc.
  • When the subquery returns no rows, ALL evaluates to TRUE for comparisons like > (vacuous truth), but behavior can vary — test carefully.

? Syntax / Theory

? View Code Example
// Syntax examples for ANY and ALL with a subquery
SELECT column1
FROM table1
WHERE column2 operator ANY (SELECT column2 FROM table2);
SELECT column1
FROM table1
WHERE column2 operator ALL (SELECT column2 FROM table2);

? Code Example(s)

? View Code Example
// Find employees whose salary is greater than any salary in the IT department
SELECT employee_name, salary
FROM employees
WHERE salary > ANY (SELECT salary FROM employees WHERE department = 'IT');
? View Code Example
// Find products with price greater than all prices in Electronics category
SELECT product_name, price
FROM products
WHERE price > ALL (SELECT price FROM products WHERE category = 'Electronics');

? Explanation

  • ANY: Returns TRUE if the comparison is satisfied by at least one value from the subquery. Example: salary > ANY (subquery) is true if salary is greater than one or more returned values.
  • ALL: Returns TRUE only if the comparison is satisfied by every value from the subquery. Example: salary > ALL (subquery) is true if salary is greater than every value returned.
  • Behavior: Use ANY for "exists a value" style checks, and ALL for "for all values" style checks. Be mindful of empty subqueries and NULLs in the result set.

Live Output / Explanation

These queries compare scalar values against the set produced by the subquery. For example, if the IT department has salaries [40k, 50k], then salary > ANY (...) returns rows where salary > 40k (since 40k is in the set), while salary > ALL (...) returns rows where salary > 50k (greater than every value).

✅ Tips & Best Practices

  • Prefer ANY when you need a match against any member of a set; prefer ALL when you need to ensure the value exceeds or meets every member.
  • Remember NULLs: if the subquery returns NULLs, comparisons may evaluate to UNKNOWN — filter NULLs in the subquery if needed.
  • Test queries on representative data to ensure they reflect intended logic.

? Try It Yourself / Practice Tasks

  • Write a query to find customers who have orders with total_amount greater than any total_amount from orders in another category.
  • Write a query to find employees with salary less than all salaries in a given department.
  • Experiment by adding AND / OR conditions around ANY and ALL to see interaction effects.