PostgreSQL – SOME Operator

PostgreSQL has an SOME operator that is used to compare a scalar value with a set of values returned by a subquery.

Syntax: expression operator SOME(subquery)

The below rules must be followed while using PostgreSQL SOME operator:

  • The subquery must return exactly one column.
  • The SOME operator must be preceded by one of the following comparison operator =, <=, >, <, > and <>
  • The SOME 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.

Example 1:

Here we will query for the maximum length of film grouped by film category from the “film” table of our sample database.

SELECT title
FROM film
WHERE length >= SOME(
    SELECT MAX( length )
    FROM film
    INNER JOIN film_category USING(film_id)
    GROUP BY  category_id );

Output:

Example 2:
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.

SELECT
    title,
    category_id
FROM
    film
INNER JOIN film_category
        USING(film_id)
WHERE
    category_id = SOME(
        SELECT
            category_id
        FROM
            category
        WHERE
            NAME = 'Action'
            OR NAME = 'Drama'
    );

Output:

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.