ANY

ANY allows a WHERE or HAVING condition to pass if any of its subquery conditions are true. It returns TRUE if any of the subquery values satisfy the condition. ANY must be accompanied by a comparison operator.

Example

SELECT employee_name
FROM employee
WHERE employee_salary > ANY
(SELECT employee_salary
FROM employee
WHERE department_id = 3);

Output

Mike
John
Doe

Explanation

The SQL ANY operator is used to compare a value to any applicable value in the list. In the provided query, the ANY operator is used in a subquery to fetch any employee names from the employee table where employee_salary is greater than the salary of any employee in the department_id = 3. The output is the list of employees who satisfy this condition.

Example

SELECT * FROM Employees WHERE Salary > ANY (SELECT Salary FROM Managers);

Output

| Name | Position | Salary |
|-------|----------|--------|
| Bob | Clerk | 50000 |
| Carol | Designer | 55000 |

Explanation

The ANY operator in the SELECT statement is used to compare a value with any value in a list. In this case, it returns employees who are earning more than any manager.

Example

SELECT employee_name
FROM Employees
WHERE salary > ANY
(
SELECT salary
FROM Departments
WHERE dept_name = 'Sales'
);

Output

employee_name
---------------
Mark
John
Sophia
Linda

Explanation

The ANY operator in SQL is used to compare a value with any or some of a set of values. In this example, it’s used to select employee names from the Employees table where their salary exceeds the salary of any employee in the Sales department. If any of the salaries in the Sales department are less than an employee’s salary in the broader Employees table, that employee’s name will be included in the output.

Example

SELECT first_name, last_name
FROM employees
WHERE salary > ANY (SELECT salary
FROM employees
WHERE job_id = 'IT_PROG');

Output

FIRST_NAME | LAST_NAME
-----------|----------
Steven | King
Neena | Kochhar
Lex | De Haan
Alexander | Hunold
Bruce | Ernst

Explanation

In this example, the ‘ANY’ keyword is used in a subquery to compare the salary of each record in the ‘employees’ table to the salaries of employees with the ‘IT_PROG’ job id. If an employee’s salary is greater than any of these salaries, their first and last name are selected for the output.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.