PostgreSQL – ALL Clause
The ALL
clause is typically used in conjunction with comparison operators to compare a value against all values in a set returned by a subquery.The ALL
clause returns TRUE
if the condition is satisfied for all values in the set.
Here’s the basic syntax of using the ALL
clause:
SELECT columns FROM table_name WHERE expression operator ALL (subquery);
For example, if you want to find products with prices greater than all prices in a certain category:
SELECT product_id, product_name, price FROM products WHERE price > ALL ( SELECT price FROM products WHERE product_category = 'Books' );
In this query, the price > ALL
clause ensures that the price of the product in the outer query is greater than all the prices in the subquery, effectively finding products with prices higher than any price in the “Books” category.
Keep in mind that using ALL
in this way requires that the condition holds true for all values in the set returned by the subquery. If any comparison fails, the result will be FALSE
.