---
title: "List predicates: ALL, ANY, NONE and SINGLE"
description: "Test the elements of a collection in CoraDB SQL with the ALL, ANY, NONE and SINGLE list predicates: syntax, NULL rules, examples."
url: "https://www.coradb.ai/manual/sql/function/predicate_fn.html"
markdown_url: "https://www.coradb.ai/manual/markdown/sql/function/predicate_fn.md"
product: "CoraDB"
version: "1.0"
keywords: "coradb all, coradb any, coradb none, coradb single, list predicate, collection predicate, sequence, set, multiset"
---

# List predicates: ALL, ANY, NONE and SINGLE

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 ...)`.

```sql
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.

> **Empty collections and NULL elements:**
> - On an empty collection, `ALL` and `NONE` return `1`; `ANY` and `SINGLE` return `0`.
> - An element for which the condition is not true (including when it is `NULL` because the element is `NULL`) counts as **not satisfying** it. The predicates never return `NULL`: `ALL(x IN {3, NULL} WHERE x > 2)` is `0` and `NONE(x IN {1, NULL} WHERE x > 2)` is `1`.
> - A collection literal made only of `NULL` values, or mixing `NULL` with constants, has no element type and is rejected (`Cannot coerce ... to type unknown data type`). Store such values in a typed collection column instead.


## ALL

`ALL` returns `1` when every element satisfies the condition.

```sql
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:

```sql
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

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

```sql
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 |

> **Warning:**
> Inside a list predicate, `ANY` takes the form `ANY(variable IN collection WHERE condition)`. The comparison form `expression = ANY (subquery)` is a different construct, described in [Conditional expressions](https://www.coradb.ai/manual/sql/function/condition_op.html#any-some-all).


## NONE

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

```sql
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

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

```sql
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:

```sql
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 |

## All four on the same collections

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

```sql
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 |

## Empty collections

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:

```sql
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)`.

## Collection columns and NULL elements

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:

```sql
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 |

## See also

- [Conditional expressions (ANY/ALL with subqueries, IN, EXISTS, ...)](https://www.coradb.ai/manual/sql/function/condition_op.html)
- [Collection data types](https://www.coradb.ai/manual/sql/datatype_collection.html#collection-types)
- [Containment operators (SUBSET, SUPERSET, ...)](https://www.coradb.ai/manual/sql/function/containment_op.html)
- [List functions](https://www.coradb.ai/manual/sql/function/list_fn.html)
