PostgreSQL – ANY Operator
PostgreSQL has an ANY operator that is used to compare a scalar value with a set of values returned by a subquery.
Syntax: expression operator ANY(subquery)
The below rules must be followed while using PostgreSQL ANY operator:
- The subquery must return exactly one column.
- The ANY operator must be preceded by one of the following comparison operator =, <=, >, <, > and <>
- The ANY operator returns true if any value of the subquery meets the condition, otherwise, it returns false.
For the sake of this article we will be using the sample DVD rental database, which is explained here and can be downloaded by clicking on this link in our examples.
Here we will query for the maximum length of film grouped by film category from the “film” table of our sample database.
WHERE length >= ANY(
SELECT MAX( length )
INNER JOIN film_category USING(film_id)
GROUP BY category_id );
Here we will query for the films whose category is either Action(category_id = 1) or Drama(category_id = 7) from the “category” table of our sample database.
INNER JOIN film_category
category_id = ANY(
NAME = 'Action'
OR NAME = 'Drama'