Page Content

Tutorials

What Is ANY Operator In PostgreSQL With Code Example

ANY Operator in PostgreSQL

The PostgreSQL ANY operator returns true if a comparison operator returns true for at least one value in a set. These data can come from a subquery that returns a single column or an array expression. If col is 5, 7, or 9, col = ANY(5,7,9) returns true. With an array, a > ANY (ARRAY[x, y, z]) means (a > x OR a > y OR a > z). SOME and IN are synonyms for ANY and = ANY, respectively. SQL’s three-valued logic applies to NULL values: if no valid comparison is detected and at least one value in the comparison set (from a subquery or array) is NULL, the ANY construct returns NULL (unknown).

Two main ways to use the ANY operator are as follows:

  • operator ANY for expressions (subquery)
  • ANY is an expression operator for an array.
ANY Operator in PostgreSQL
ANY Operator in PostgreSQL

ANY with a Subquery

The ANY operator and its synonym SOME are strong PostgreSQL constructs for comparing subquery values. In the form expression operator ANY (subquery), the subquery must return one column, and the left-hand expression is compared to each row of the result using the Boolean operator. The ANY construct returns true if the comparison operator returns true for any value in the subquery’s result set and false otherwise.

ANY can compare subqueries that return multiple rows, unlike normal comparison operators. IN is like = ANY. If no true comparison is made and at least one value in the subquery’s result set is NULL, the ANY construct returns NULL (unknown), not false, following SQL’s three-valued semantics. PostgreSQL may stop execution when a true result is found, so don’t assume the subquery will be fully analysed.

In recursive Common Table Expressions (CTEs), a pattern like g.id = ANY(path) can efficiently check if a node has been visited in an array representing the path for cycle detection. The left-hand side of PostgreSQL can be a row constructor to compare row-wise with subquery results if the subquery returns the same amount of columns. Due to grammatical ambiguity with its subquery comparison operator role, PostgreSQL does not implement ANY or several as aggregate functions, unlike several SQL standards.

ANY with an Array Expression

When used with an array expression in PostgreSQL, the ANY operator compares each element of an array to a single expression and returns true if the comparison is true for at least one array element. This form uses the expression operator ANY (array expression) as its syntax. For instance, if an is greater than x, y, or z, the condition a > ANY (ARRAY[x, y, z]) will yield true since it is logically equal to (a > x OR a > y OR a > z). Likewise, SELECT 1 = ANY (‘{1,2,3}’::INT[]); will return true as the given integer array {1,2,3} contains the number 1.

Any and SOME are direct synonyms that have the same meaning. The behaviour of the ANY operator when NULL values are present is consistent with SQL’s three-valued logic: the ANY result will be NULL if the array expression itself yields a NULL array or if the left-hand expression is NULL (presuming a strict comparison operator). Additionally, the overall result of ANY will be NULL (unknown), not false, if no comparison with an array element returns true and at least one array element is NULL.

This operator can be used in recursive searches for tasks like cycle detection by determining whether the ID of a current node occurs ANYwhere in the path array. It is especially helpful for searching within array columns, providing a condensed substitute for many OR tests. Multidimensional arrays of different base, enum, or composite types can be supported by PostgreSQL’s mature array data type, which also offers a wide range of operators and methods for querying and processing.

Synonyms and Equivalences

The ANY operator in PostgreSQL provides obvious equivalencies and direct synonyms that make query building easier and improve comprehension of its logic. Since the keywords SOME and ANY are direct synonyms, they can be used interchangeably and act in expressions in the same way when comparing against array elements or subquery results. Furthermore, the IN operator and = ANY are functionally equal. For instance, the shorthand expression IN (value1, value2,…) reflects the behaviour of = ANY and can be used to express expression = value1 OR expression = value2 OR… Similarly, <> ALL() is the same as NOT IN().

Since a > ANY (ARRAY[x, y, z]) is equal to (a > x OR a > y OR a > z), the fundamental logic of ANY is sometimes thought of as an OR condition over the set of values it compares against. Because of a possible syntactic issue with their use as subquery comparison operators, PostgreSQL does not implement ANY and SOME by name in this context, even though SQL standards include them as aggregate functions. Instead, bool_or performs a comparable job for boolean aggregation. This makes it clear when parsing if ANY is serving as an aggregate function or introducing a subquery comparison.

Handling of NULL Values

A result can be true, false, or NULL (which stands for “unknown”), and PostgreSQL’s ANY operator handles NULL values strictly in line with SQL’s three-valued theory. The ANY construct will return NULL if the array expression or subquery itself evaluates to a NULL array or yields no rows. Likewise, unless a non-strict comparison operator is applied, the result is often NULL if the left-hand expression in the ANY comparison returns NULL. The fact that standard comparison operators, such = or <>, return NULL when either input is NULL, indicating an undetermined result, is a crucial component of NULL handling.

As a result, the overall ANY construct will evaluate to NULL (unknown) rather than false if no comparison with any element or row in the set returns true and at least one of these comparisons returns NULL (due to a NULL value within the array or subquery). Accordingly, if all comparisons against the collection of values expressly return false and none of these comparisons result in NULL, ANY will only unquestionably return false. Notably, false OR NULL evaluates to NULL, whereas true OR NULL evaluates to true.

PostgreSQL-Specific Considerations

Pseudotypes vs. Operator: PostgreSQL defines functions that can take generic data types using a variety of polymorphic pseudotypes, including any, anyarray, and anyelement. Function arguments and return types can be more flexible with these pseudotypes (value anyelement in check_not_null function, for example). The ANY operator, which is used for comparisons in SQL query expressions, is different from these, though.

ANY as an Aggregate (Not Supported by Name): Despite the fact that SQL standards provide ANY as an aggregate function (much like bool_or), PostgreSQL does not directly implement an aggregate with the name ANY. A possible syntactic ambiguity with the subquery/array comparison operator ANY is the cause of this. Instead, aggregates are like bool_or, which returns true if one input value is true.

Recursive CTEs and Cycle Detection: Many recursive Common Table Expressions (CTEs) use ANY to find cycles in graph-like data. For example, g.id = ANY(path) can be used to prevent infinite loops by determining whether the current node, g.id, has already been met in the route array during recursion. Avoiding the ROW() syntax and using a simple array rather than a composite-type array is advised for the performance of single-field cycle detection in recursive CTEs.

Code Example:

CREATE TEMP TABLE graph (
    id   INT,
    next INT
);
INSERT INTO graph VALUES
(1, 2),
(2, 3),
(3, 1),
(3, 4);
WITH RECURSIVE search_path(id, path) AS (
    SELECT id, ARRAY[id]
    FROM graph
    WHERE id = 1
    UNION ALL
    SELECT g.next, path || g.next
    FROM graph g
    JOIN search_path sp ON g.id = sp.id
    WHERE NOT g.next = ANY(sp.path)  
)
SELECT * FROM search_path;

Output:

CREATE TABLE
INSERT 0 4
 id |   path    
----+-----------
  1 | {1}
  2 | {1,2}
  3 | {1,2,3}
  4 | {1,2,3,4}
(4 rows)

Conclusion

Finally, PostgreSQL ANY operator allows flexible comparisons across arrays or subquery results, acting as an OR condition over many variables. This implements SQL’s three-valued logic, returning true if at least one match is discovered, false if all comparisons fail, and NULL if null values prevent a decisive result. Similar to IN and SOME, it simplifies query writing and improves readability. ANY is crucial to recursive queries like cycle detection in graph traversal, which eliminates infinite loops by checking if a node exists in a path. Any is not an aggregate function in PostgreSQL due to syntactic ambiguity, although bool_or does. Set-based logic requires the operator, making PostgreSQL queries more concise, efficient, and expressive.

Kowsalya
Kowsalya
Hi, I'm Kowsalya a B.Com graduate and currently working as an Author at Govindhtech Solutions. I'm deeply passionate about publishing the latest tech news and tutorials that bringing insightful updates to readers. I enjoy creating step-by-step guides and making complex topics easier to understand for everyone.
Index