Skip to content

List predicates: ALL, ANY, NONE and SINGLE

View as Markdown

The list predicates test every element of a collection (a SET, MULTISET or SEQUENCE value) against a condition and return 1 (true) or 0 (false). They are useful on collection columns and on collections built from a subquery with SEQUENCE(SELECT ...).

ALL ( variable IN collection WHERE condition )
ANY ( variable IN collection WHERE condition )
NONE ( variable IN collection WHERE condition )
SINGLE ( variable IN collection WHERE condition )
  • variable: a name that stands for the current element inside condition. It is visible only inside the predicate.
  • collection: a collection literal such as {1, 2, 3}, a collection column, or SEQUENCE(subquery).
  • condition: a Boolean expression, normally using variable. It can also refer to columns of the outer query.
Predicate Returns 1 when
ALL Every element satisfies the condition
ANY At least one element satisfies the condition
NONE No element satisfies the condition
SINGLE Exactly one element satisfies the condition

The result type is INTEGER, so a predicate can be used in the select list, in WHERE and HAVING, and anywhere else a Boolean expression is allowed.

ALL returns 1 when every element satisfies the condition.

SELECT ALL(x IN {1, 2, 3, 4} WHERE x > 2), ALL(x IN {3, 4, 5, 6} WHERE x > 2);
ALL(x IN {1, 2, 3, 4} WHERE x > 2) ALL(x IN {3, 4, 5, 6} WHERE x > 2)
0 1

Orders in which no line has a discount:

SELECT COUNT(*)
FROM orders o
WHERE ALL(x IN SEQUENCE(SELECT discount_pct FROM order_items i WHERE i.order_id = o.id) WHERE x = 0);
COUNT(*)
927

ANY returns 1 when at least one element satisfies the condition.

SELECT ANY(x IN {-1, 0, 1, 2} WHERE x > 2), ANY(x IN {3, 4, 5, 6} WHERE x > 2);
ANY(x IN {-1, 0, 1, 2} WHERE x > 2) ANY(x IN {3, 4, 5, 6} WHERE x > 2)
0 1

NONE returns 1 when no element satisfies the condition. It is the negation of ANY.

SELECT NONE(x IN {1, 2, 3, 4} WHERE x > 2), NONE(x IN {-1, 0, 1, 2} WHERE x > 2);
NONE(x IN {1, 2, 3, 4} WHERE x > 2) NONE(x IN {-1, 0, 1, 2} WHERE x > 2)
0 1

SINGLE returns 1 when exactly one element satisfies the condition, and 0 when none or more than one do.

SELECT SINGLE(x IN {1, 2, 3, 4} WHERE x > 2), SINGLE(x IN {0, 1, 2, 3, -1} WHERE x > 2);
SINGLE(x IN {1, 2, 3, 4} WHERE x > 2) SINGLE(x IN {0, 1, 2, 3, -1} WHERE x > 2)
0 1

Orders that have exactly one line with a quantity of 10 or more:

SELECT COUNT(*)
FROM orders o
WHERE SINGLE(x IN SEQUENCE(SELECT quantity FROM order_items i WHERE i.order_id = o.id) WHERE x >= 10);
COUNT(*)
147

The quantities of the lines of the first eight orders, tested with each predicate:

SELECT o.id,
SEQUENCE(SELECT quantity FROM order_items i WHERE i.order_id = o.id) AS quantities,
ALL(x IN SEQUENCE(SELECT quantity FROM order_items i WHERE i.order_id = o.id) WHERE x >= 2) AS all_ge_2,
ANY(x IN SEQUENCE(SELECT quantity FROM order_items i WHERE i.order_id = o.id) WHERE x >= 10) AS any_ge_10,
NONE(x IN SEQUENCE(SELECT quantity FROM order_items i WHERE i.order_id = o.id) WHERE x >= 10) AS none_ge_10,
SINGLE(x IN SEQUENCE(SELECT quantity FROM order_items i WHERE i.order_id = o.id) WHERE x = 1) AS single_eq_1
FROM orders o
WHERE o.id < 8
ORDER BY o.id;
id quantities all_ge_2 any_ge_10 none_ge_10 single_eq_1
0 {1, 1, 1, 1} 0 0 1 0
1 {2} 1 0 1 0
2 {2, 4} 1 0 1 0
3 {1, 1, 12} 0 1 0 0
4 {2, 7, 2} 1 0 1 0
5 {2} 1 0 1 0
6 {1, 4} 0 0 1 1
7 {1, 1, 1, 9, 9, 1, 2} 0 0 1 0

A collection built from a correlated subquery is empty when the subquery finds no rows. 288 products have never been ordered, so for them the collection of order quantities is empty, and ALL is true whatever the condition:

SELECT COUNT(*)
FROM product p
WHERE ALL(x IN SEQUENCE(SELECT quantity FROM order_items i WHERE i.product_id = p.id) WHERE x >= 100);
COUNT(*)
288

No order line has a quantity of 100 or more, so the 288 products counted are exactly the products that were never ordered. To exclude empty collections, add a condition such as EXISTS (SELECT 1 FROM order_items i WHERE i.product_id = p.id).

With a table that stores a typed SEQUENCE column, the predicates read the column directly. The rows below show how NULL elements and an empty collection are handled:

CREATE TABLE pred_demo (id INT PRIMARY KEY, s SEQUENCE(INT));
INSERT INTO pred_demo VALUES (1, {3, 4}), (2, {3, NULL}), (3, {1, NULL}), (4, {NULL, NULL}), (5, {});
SELECT id, s,
ALL(x IN s WHERE x > 2) AS all_gt_2,
ANY(x IN s WHERE x > 2) AS any_gt_2,
NONE(x IN s WHERE x > 2) AS none_gt_2,
SINGLE(x IN s WHERE x > 2) AS single_gt_2
FROM pred_demo ORDER BY id;
id s all_gt_2 any_gt_2 none_gt_2 single_gt_2
1 {3, 4} 1 1 0 0
2 {3, NULL} 0 1 0 1
3 {1, NULL} 0 0 1 0
4 {NULL, NULL} 0 0 1 0
5 {} 1 0 1 0