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:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 Aug, 2020
Like Article
Save Article