capypad
0 day streak
sql / expert
Snippet

Quantified Comparisons for Global Set Constraints

The ALL predicate is an ANSI SQL quantified comparison that evaluates to true only if the comparison holds for every row in the subquery result set. It provides a more readable and declarative alternative to MAX() comparisons when dealing with correlated subqueries or empty sets.

snippet.sql
sql
1
2
3
4
5
6
7
SELECT account_id, balance
FROM ledger
WHERE balance > ALL (
SELECT balance
FROM risk_thresholds
WHERE category = 'EXTREME'
);
Breakdown
1
WHERE balance > ALL
Specifies that the balance must be greater than every single value returned by the following set.
2
SELECT balance FROM risk_thresholds
The subquery defining the set of values for the universal quantification.