capypad
0 day streak
sql / expert
Snippet

Quantified Comparison Predicates (ALL/ANY)

The ALL predicate is an advanced SQL feature used to compare a value against every value returned by a subquery. In this case, it retrieves products that are more expensive than every single product in category 5, offering a more readable alternative to MAX() subqueries.

snippet.sql
sql
1
2
3
4
5
6
7
SELECT product_name, unit_price
FROM products
WHERE unit_price > ALL (
SELECT unit_price
FROM products
WHERE category_id = 5
);
Breakdown
1
WHERE unit_price > ALL (...)
The condition evaluates to true only if the price is greater than every value in the set.
2
SELECT unit_price FROM products WHERE category_id = 5
The subquery defining the set of values for comparison.