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.
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;
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;