The PostgreSQL ANY operator compares a value to a set of values returned by a subquery using a comparison operator. It returns true if the comparison holds for at least one value in the set. SOME is an alias for ANY and behaves identically.
ANY operator syntax
expression operator ANY (subquery)
expression— the value to compare (typically a column).operator— any comparison operator:=,<>,>,>=,<,<=.subquery— must return exactly one column. If it returns zero rows, ANY is always false.
Example: using ANY with the = operator
This query finds employees whose salary matches any manager's salary:
SELECT *
FROM employees
WHERE salary = ANY (
SELECT salary
FROM managers
);
Output:
id | first_name | last_name | salary
----+------------+-----------+----------
2 | Charlie | Davis | 55000.00
(1 row)
Charlie's salary of 55,000 matches one of the manager salaries (55K, 58K, 60K), so he is returned. value = ANY (subquery) is equivalent to value IN (subquery).
Example: using ANY with the > operator
This query finds employees who earn more than at least one manager's salary:
SELECT *
FROM employees
WHERE salary > ANY (
SELECT salary
FROM managers
);
Output:
id | first_name | last_name | salary
----+------------+-----------+----------
9 | Jack | Anderson | 56000.00
11 | Liam | Clark | 59000.00
(2 rows)
Jack (56K) is greater than the lowest manager salary (55K), so he qualifies. Liam (59K) is greater than both 55K and 58K.
Example: using ANY with the < operator
This query finds employees who earn less than at least one manager — in practice, almost everyone qualifies:
SELECT *
FROM employees
WHERE salary < ANY (
SELECT salary
FROM managers
);
Output includes 11 of 12 employees, because any salary below the highest manager salary (60K) satisfies the condition for at least one manager value.
Tips for using ANY effectively
= ANYis interchangeable withINfor subqueries. Use whichever reads more clearly for your team.<> ANYis NOT the same asNOT IN.<> ANYreturns true if the value differs from at least one element in the set (which is almost always true). UseNOT INor<> ALLto exclude rows that match any value.- If the subquery can return
NULLvalues and you use= ANY, rows with a null in the set do not cause the comparison to fail — but they will not match either. Be aware of nullability in the subquery column. - For a fixed list of values,
ANY(ARRAY[val1, val2, val3])is a clean alternative to a subquery.
Reference: PostgreSQL documentation — Subquery Expressions (ANY/SOME).