MySQL ANY and ALL Operators (original) (raw)

Last Updated : 26 Mar, 2026

The ANY and ALL operators in MySQL are used with subqueries to compare a value against a set of values returned by another query. They help in writing flexible and powerful conditions when working with multiple rows.

First, we create a demo table on which the ANY and ALL operators will be used:

Screenshot-2026-03-26-140633

employees Table

ANY Operator

The ANY operator checks whether a comparison is true for any one of the values returned by a subquery.

**Note: The ANY operator is also known as SOME in MySQL, and both can be used interchangeably.

**Syntax:

SELECT column_name FROM table_name WHERE column_name operator ANY (subquery);

**Example: In below example we will find employees whose salary is greater than the salary of any employee in the 'HR' department.

SELECT name, salary FROM employees WHERE salary > ANY (SELECT salary FROM employees WHERE department = 'HR');

**Output:

Screenshot-2026-03-26-141542

ALL Operator

The ALL operator in MySQL is used to check whether a condition is true for all values returned by a subquery. It ensures that the comparison holds true for every value in the result set.

**Syntax:

SELECT column_name FROM table_name WHERE column_name comparison_operator ALL (subquery);

**Example: In below example we will find employees whose salary is greater than the salary of all employees in the 'HR' department.

SELECT name, salary FROM employees WHERE salary > ALL (SELECT salary FROM employees WHERE department = 'HR');

**Output:

Screenshot-2026-03-26-141845

Combining ANY and ALL with Other MySQL Clauses

The ANY and ALL operators can be combined with other MySQL clauses to create more advanced and meaningful queries. They are often used with clauses like JOIN, GROUP BY, and ORDER BY to refine data retrieval and analysis.

Example 1: Using ANY with JOIN

Find employees who earn more than any employee in a different department, and list their department details. Consider the departments table as shown below:

Screenshot-2026-03-26-144835

departments Table

**Query:

SELECT e.employee_id, e.name, d.department_name
FROM employees e
JOIN departments d
ON e.department = d.department_name
WHERE e.salary > ANY (
SELECT salary
FROM employees
WHERE department = 'Sales'
);

**Output:

Screenshot-2026-03-26-145042

Example 2: Using ALL with GROUP BY

Find departments where all employees have salary greater than 50000.

**Query:

SELECT department
FROM employees
GROUP BY department
HAVING MIN(salary) > ALL (
SELECT salary
FROM employees
WHERE salary <= 50000
);

**Output:

Screenshot-2026-03-26-145205

Example 3: Using ANY with ORDER BY

Find employees who earn more than any employee in HR, and display them in descending salary order.

**Query:

SELECT name, salary
FROM employees
WHERE salary > ANY (
SELECT salary
FROM employees
WHERE department = 'HR'
)
ORDER BY salary DESC;

**Output:

Screenshot-2026-03-26-145256