The ANY operator in PostgreSQL is used to compare a value with a set of values returned by a subquery or an array. The condition evaluates to TRUE if at least one value in the set satisfies the comparison.
- Supports comparison operators such as =, >, <, >=, <= and <>.
- Commonly used with subqueries.
Syntax
SELECT column_1, column_2, ...
FROM table_name
WHERE expression comparison_operator ANY (
subquery
);
Where:
- expression: The value to compare.
- comparison_operator: Operators such as =, >, <, >=, <= or <>.
- ANY: Returns TRUE if at least one comparison is true.
- subquery: Returns a single-column result set.
Examples
Firstly, create the Department and employee tables and insert some records.
employee table:

department table:

Example 1: Basic Use with an Array
SELECT * FROM employee
WHERE salary = ANY (ARRAY[45000, 65000]);
Output:

Example 2: Use with a Subquery
SELECT employee_name
FROM employee
WHERE salary > ANY (
SELECT min_salary
FROM department
);
Output:

This query returns employees whose salary is greater than at least one department's minimum salary.
Example 3: Use with NOT ANY
SELECT * FROM employee
WHERE NOT (salary = ANY (ARRAY[45000, 65000]));
Output:

This query retrieves employees whose salary is not equal to 45000 or 65000.
Example 4: Combining Conditions
SELECT employee_name, salary
FROM employee
WHERE salary > ANY (ARRAY[40000, 60000])
AND employee_id > 101;
Output:

This query returns employees whose salary is greater than at least one value in the array and whose employee_id is greater than 101.

