PostgreSQL – ALL Operator

The PostgreSQL ALL operator is used for comparing a value with a list of values returned by a subquery.

Syntax: comparison_operator ALL (subquery)

The below rules need to be followed while using the ALL operator:

  • The ALL operator always needs to be preceded by a comparison operator(=, !=, <, >, >=, <=).
  • It must always be followed by a subquery surrounded by parentheses.

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 all films whose lengths are greater than the list of the average lengths by using the ALL and greater than operator(>).

SELECT
    film_id,
    title,
    length
FROM
    film
WHERE
    length > ALL (
            SELECT
                ROUND(AVG (length), 2)
            FROM
                film
            GROUP BY
                rating
    )
ORDER BY
    length;

Output:

Example 2:

Here we will query for all films whose rental_rate is less than the list of the average rental_rate by using the ALL and less than operator(<).

SELECT
    film_id,
    title,
    rental_rate
FROM
    film
WHERE
    rental_rate < ALL (
            SELECT
                ROUND(AVG (rental_rate), 2)
            FROM
                film
            GROUP BY
                rating
    )
ORDER BY
    rental_rate;


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.