

# List search and set functions

Use these functions to test whether a list contains a value or some or all elements of a second list, and to find the position of a value. You can also remove duplicate values, count distinct values, or find the elements that two lists share.

## `&&` (overlap) operator
<a name="athena-duckdb-functions-list-overlap-operator"></a>

Returns `true` if `list1` and `list2` share at least one element, and `false` if they do not. Elements are compared by value, so the order of the elements and the number of times that each one appears do not matter. You can also use the operator with two `ARRAY` values, and the two values do not need to have the same number of elements.

`NULL` elements never match, not even a `NULL` element in the other list. An empty list has no element in common with any list, so the result is `false`. If either list is `NULL`, the result is `NULL`.

`{{list1}} && {{list2}}` returns the same result as `list_has_any({{list1}}, {{list2}})`.

### Syntax
<a name="athena-duckdb-functions-list-overlap-operator-syntax"></a>

```sql
{{list1}} && {{list2}}
```

### Arguments
<a name="athena-duckdb-functions-list-overlap-operator-arguments"></a>

The `&&` (overlap) operator takes the following arguments:

**{{list1}}**  
The first list, or an `ARRAY` value.

**{{list2}}**  
The second list, or an `ARRAY` value. Its elements must have the same type as the elements of `list1`, or a type that DuckDB can convert to a common type with them.

### Return type
<a name="athena-duckdb-functions-list-overlap-operator-return-type"></a>

`BOOLEAN`

### Examples
<a name="athena-duckdb-functions-list-overlap-operator-examples"></a>

The following SQL query returns `true` because both lists contain `3`.

```sql
SELECT [1, 2, 3] && [3, 4] AS result
```

The query returns the following result.


| result | 
| --- | 
| `true` | 

The following SQL query returns `false` twice. The first pair of lists has no element in common. In the second pair, `b` and `B` differ in case, so they do not match.

```sql
SELECT
    [1, 2] && [3, 4] AS no_shared_element,
    ['a', 'b'] && ['B', 'C'] AS different_case
```

The query returns the following result.


| no\_shared\_element | different\_case | 
| --- | --- | 
| `false` | `false` | 

The following SQL query shows that `NULL` elements do not match. Both results are `false`, even though both lists in the second pair contain `NULL`.

```sql
SELECT
    [1, NULL] && [2] AS no_shared_value,
    [1, NULL] && [NULL, 3] AS only_null_shared
```

The query returns the following result.


| no\_shared\_value | only\_null\_shared | 
| --- | --- | 
| `false` | `false` | 

The following SQL query uses `&&` in a `WHERE` clause to return the rows whose `colors` list contains `blue` or `black`.

```sql
SELECT id
FROM (VALUES
    (1, ['red', 'blue']),
    (2, ['green']),
    (3, ['blue', 'white'])
) AS t(id, colors)
WHERE colors && ['blue', 'black']
ORDER BY id
```

The query returns the following results.


| id | 
| --- | 
| `1` | 
| `3` | 

### Considerations
<a name="athena-duckdb-functions-list-overlap-operator-considerations"></a>

If one operand is a list or an `ARRAY` value, the other operand must be one too, so `[1, 2] && 1` fails. To test whether a list contains one value, use the [`list_contains` function](#athena-duckdb-functions-list-list-contains).

If the element types cannot be converted to a common type, as in `[1, 2] && ['a']`, the query fails.

String elements match only when they have the same case, so `['a', 'b'] && ['B', 'C']` returns `false`.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-list-overlap-operator-differences-from-engine-version-3"></a>

Athena engine version 3 uses the `arrays_overlap` function for this test, and DuckDB has no `arrays_overlap` function. Replace `arrays_overlap({{array1}}, {{array2}})` with `{{array1}} && {{array2}}` or `list_has_any({{array1}}, {{array2}})`.

In Athena engine version 3, `arrays_overlap` returns `NULL` when the two arrays have no element in common other than `NULL` and either array contains a `NULL` element. In DuckDB, `&&` returns `false` in that case, so `[1, NULL] && [2]` returns `false`.

## `list_contains` function
<a name="athena-duckdb-functions-list-list-contains"></a>

Returns `true` if at least one element of `list` equals `element`, and `false` if none does. Elements are compared with `=`, so string comparisons are case-sensitive. A `NULL` element never matches, so `list_contains([1, NULL], 3)` returns `false`, not `NULL`.

If `list` or `element` is `NULL`, the result is `NULL`. For this reason, you cannot use `list_contains` to find a `NULL` element. To test whether a list holds a `NULL` element, use `list_count({{list}}) < len({{list}})`, which returns `true` when at least one element is `NULL`.

`array_contains`, `array_has`, and `list_has` are aliases of `list_contains`.

### Syntax
<a name="athena-duckdb-functions-list-list-contains-syntax"></a>

```sql
list_contains({{list}}, {{element}})
```

### Arguments
<a name="athena-duckdb-functions-list-list-contains-arguments"></a>

The `list_contains` function takes the following arguments:

**{{list}}**  
The list to search.

**{{element}}**  
The value to look for. Its type must be comparable with the element type of `list`.

### Return type
<a name="athena-duckdb-functions-list-list-contains-return-type"></a>

`BOOLEAN`

### Examples
<a name="athena-duckdb-functions-list-list-contains-examples"></a>

The following SQL query finds the value 1 in a list that also holds a `NULL` element.

```sql
SELECT list_contains([1, 2, NULL], 1) AS result
```

The query returns the following result.


| result | 
| --- | 
| `true` | 

The following SQL query shows the two `NULL` cases. A search with no match returns `false` even though the list holds a `NULL` element, and a search for `NULL` returns `NULL`.

```sql
SELECT
    list_contains([1, NULL], 3) AS no_match,
    list_contains([1, NULL], NULL) AS search_for_null
```

The query returns the following result.


| no\_match | search\_for\_null | 
| --- | --- | 
| `false` | `NULL` | 

The following SQL query returns the rows whose list of tags includes `blue`.

```sql
SELECT order_id
FROM (VALUES
    (1, ['red', 'blue']),
    (2, ['green']),
    (3, ['blue'])
) AS t(order_id, tags)
WHERE list_contains(tags, 'blue')
ORDER BY order_id
```

The query returns the following results.


| order\_id | 
| --- | 
| `1` | 
| `3` | 

The following SQL query uses the `array_has` alias. The search is case-sensitive, so `B` does not match `b`.

```sql
SELECT array_has(['a', 'b'], 'B') AS result
```

The query returns the following result.


| result | 
| --- | 
| `false` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-list-list-contains-differences-from-engine-version-3"></a>

In Athena engine version 3, `contains({{array}}, {{element}})` returns `NULL` when no element matches and the array holds a `NULL` element. In DuckDB, `contains` and `list_contains` return `false` in that case.

## `list_distinct` function
<a name="athena-duckdb-functions-list-list-distinct"></a>

Returns a list with one copy of each distinct non-`NULL` value in `list`. `NULL` elements are removed, so a list that holds only `NULL` elements returns an empty list. The order of the elements in the result is not defined and can differ from their order in `list`. If `list` is `NULL`, the result is `NULL`.

To get a predictable order, sort the result, as in `list_sort(list_distinct({{list}}))`. To count the distinct values instead, use the [`list_unique` function](#athena-duckdb-functions-list-list-unique). `array_distinct` is an alias of `list_distinct`.

### Syntax
<a name="athena-duckdb-functions-list-list-distinct-syntax"></a>

```sql
list_distinct({{list}})
```

### Arguments
<a name="athena-duckdb-functions-list-list-distinct-arguments"></a>

The `list_distinct` function takes the following argument:

**{{list}}**  
The list whose distinct values you want, of any element type.

### Return type
<a name="athena-duckdb-functions-list-list-distinct-return-type"></a>

A list of the same type as `list`, such as `INTEGER[]` for an `INTEGER[]` list.

### Examples
<a name="athena-duckdb-functions-list-list-distinct-examples"></a>

The following SQL query removes the duplicate values and the `NULL` element from a list, and sorts the result.

```sql
SELECT
    list_sort(
        list_distinct([1, 1, NULL, -3, 1, 5])
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[-3, 1, 5]` | 

The following SQL query shows that a list that holds only `NULL` elements returns an empty list.

```sql
SELECT
    list_distinct(
        CAST([NULL, NULL] AS INTEGER[])
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[]` | 

The following SQL query uses the `array_distinct` alias and counts the result. Strings are compared case-sensitively, so `b` and `B` are different values.

```sql
SELECT
    len(array_distinct(['b', 'a', 'b', 'B'])) AS result
```

The query returns the following result.


| result | 
| --- | 
| `3` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-list-list-distinct-differences-from-engine-version-3"></a>

In Athena engine version 3, `array_distinct` keeps one `NULL` element and keeps the elements in the order in which they first appear. In DuckDB, `array_distinct` and `list_distinct` remove `NULL` elements and do not keep the original order.

## `list_has_all` function
<a name="athena-duckdb-functions-list-list-has-all"></a>

Returns `true` if every element of `list2` is also an element of `list1`, and `false` otherwise. The order of the elements and the number of times that each one occurs do not matter. `NULL` elements in either list are ignored, so if `list2` is empty or holds only `NULL` elements, the result is `true`. If either list is `NULL`, the result is `NULL`.

You can also write the test with the `@>` (contains) and `<@` (contained by) operators. The expression `{{list1}} @> {{list2}}` returns `true` when the list on the left contains every element of the list on the right. The expression `{{list2}} <@ {{list1}}` returns `true` when every element of the list on the left is in the list on the right. Both forms return the same result as `list_has_all({{list1}}, {{list2}})`.

`array_has_all` is an alias of `list_has_all`. To test whether two lists share at least one element, use the [`list_has_any` function](#athena-duckdb-functions-list-list-has-any).

### Syntax
<a name="athena-duckdb-functions-list-list-has-all-syntax"></a>

```sql
list_has_all({{list1}}, {{list2}})
{{list1}} @> {{list2}}
{{list2}} <@ {{list1}}
```

### Arguments
<a name="athena-duckdb-functions-list-list-has-all-arguments"></a>

The `list_has_all` function takes the following arguments:

**{{list1}}**  
The list to search, with elements of any type.

**{{list2}}**  
The list of elements to look for. Its element type must be compatible with the element type of `list1`.

### Return type
<a name="athena-duckdb-functions-list-list-has-all-return-type"></a>

`BOOLEAN`

### Examples
<a name="athena-duckdb-functions-list-list-has-all-examples"></a>

The following SQL query tests whether the first list contains every element of the second list.

```sql
SELECT list_has_all([1, 2, 3], [2, 3]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `true` | 

The following SQL query returns `false` when an element is missing, and shows that a `NULL` element in the second list is ignored.

```sql
SELECT
    list_has_all([1, 2, 3], [2, 4]) AS missing_element,
    list_has_all([1, 2], [1, NULL]) AS null_ignored
```

The query returns the following result.


| missing\_element | null\_ignored | 
| --- | --- | 
| `false` | `true` | 

The following SQL query uses the `@>` operator. The list on the left must contain every element of the list on the right, so swapping the lists changes the result.

```sql
SELECT
    [1, 2, 3] @> [1, 2] AS result,
    [1, 2] @> [1, 2, 3] AS reversed
```

The query returns the following result.


| result | reversed | 
| --- | --- | 
| `true` | `false` | 

The following SQL query uses the `<@` operator. Every element of the list on the left must be in the list on the right.

```sql
SELECT
    [1, 2] <@ [1, 2, 3] AS result,
    [1, 2, 3] <@ [1, 2] AS reversed
```

The query returns the following result.


| result | reversed | 
| --- | --- | 
| `true` | `false` | 

### Considerations
<a name="athena-duckdb-functions-list-list-has-all-considerations"></a>

The two lists must have element types that can be compared. For example, `list_has_all([1, 2], ['1'])` fails, because an `INTEGER` list and a `VARCHAR` list have no common type. Cast one of the lists to the type of the other first.

## `list_has_any` function
<a name="athena-duckdb-functions-list-list-has-any"></a>

Returns `true` if at least one element of `list1` is also an element of `list2`, and `false` otherwise. `NULL` elements are ignored, so two lists that share only `NULL` elements return `false`. An empty list has no element in common with any list. If either list is `NULL`, the result is `NULL`.

`array_has_any` is an alias of `list_has_any`. The `&&` (overlap) operator runs the same test on two lists. To test whether one list contains every element of another, use the [`list_has_all` function](#athena-duckdb-functions-list-list-has-all).

### Syntax
<a name="athena-duckdb-functions-list-list-has-any-syntax"></a>

```sql
list_has_any({{list1}}, {{list2}})
```

### Arguments
<a name="athena-duckdb-functions-list-list-has-any-arguments"></a>

The `list_has_any` function takes the following arguments:

**{{list1}}**  
The first list to compare, with elements of any type.

**{{list2}}**  
The second list to compare, whose element type is compatible with the element type of `list1`.

### Return type
<a name="athena-duckdb-functions-list-list-has-any-return-type"></a>

`BOOLEAN`

### Examples
<a name="athena-duckdb-functions-list-list-has-any-examples"></a>

The following SQL query tests whether two lists share an element.

```sql
SELECT list_has_any([1, 2, 3], [2, 3, 4]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `true` | 

The following SQL query returns `false` because the lists have no element in common.

```sql
SELECT list_has_any([1, 2, 3], [4, 5]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `false` | 

The following SQL query shows that `NULL` elements do not count as a match. The only element that the two lists share is `NULL`, so the result is `false`.

```sql
SELECT list_has_any([1, NULL], [NULL, 2]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `false` | 

### Considerations
<a name="athena-duckdb-functions-list-list-has-any-considerations"></a>

The two lists must have element types that can be compared. For example, `list_has_any([1, 2], ['1'])` fails, because an `INTEGER` list and a `VARCHAR` list have no common type. Cast one of the lists to the type of the other first.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-list-list-has-any-differences-from-engine-version-3"></a>

Athena engine version 3 uses `arrays_overlap` for this test. When the arrays share no element other than `NULL`, and either array has a `NULL` element, `arrays_overlap` returns `NULL`. In the same case, `list_has_any` returns `false`. DuckDB has no `arrays_overlap` function.

## `list_intersect` function
<a name="athena-duckdb-functions-list-list-intersect"></a>

Returns a list of the elements that occur in both `list1` and `list2`. Each element appears once in the result, even when it occurs more than once in either list. `NULL` elements are ignored, so a `NULL` that is in both lists is not in the result. If the lists have no element in common, the result is an empty list.

`array_intersect` is an alias of `list_intersect`.

### Syntax
<a name="athena-duckdb-functions-list-list-intersect-syntax"></a>

```sql
list_intersect({{list1}}, {{list2}})
```

### Arguments
<a name="athena-duckdb-functions-list-list-intersect-arguments"></a>

The `list_intersect` function takes the following arguments:

**{{list1}}**  
The first list, with elements of any type.

**{{list2}}**  
The second list, whose element type is compatible with the element type of `list1`.

### Return type
<a name="athena-duckdb-functions-list-list-intersect-return-type"></a>

A list of the common element type of the two lists, for example `INTEGER[]` for two `INTEGER[]` lists.

### Examples
<a name="athena-duckdb-functions-list-list-intersect-examples"></a>

The following SQL query returns the elements that two lists share. `list_sort` puts them in ascending order, because the order of the elements that `list_intersect` returns is not defined.

```sql
SELECT
    list_sort(
        list_intersect([1, 2, 3], [2, 3, 4])
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[2, 3]` | 

The following SQL query shows that each shared element appears once, even when it repeats. `list_sort` puts the result in a predictable order.

```sql
SELECT
    list_sort(
        list_intersect([5, 1, 5, 3, 1], [1, 5, 9])
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[1, 5]` | 

The following SQL query shows that a `NULL` element in both lists is not part of the result.

```sql
SELECT list_intersect([1, NULL, 2], [NULL, 2]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[2]` | 

The following SQL query shows the different results for a `NULL` first list and a `NULL` second list. When the first list is `NULL`, the result is `NULL`, so `IS NULL` returns `true`. When only the second list is `NULL`, the result is an empty list, so `len` returns `0`.

```sql
SELECT
    list_intersect(
        NULL, [1, 2]
    ) IS NULL AS first_is_null,
    len(
        list_intersect([1, 2], NULL)
    ) AS second_null_count
```

The query returns the following result.


| first\_is\_null | second\_null\_count | 
| --- | --- | 
| `true` | `0` | 

### Considerations
<a name="athena-duckdb-functions-list-list-intersect-considerations"></a>

The order of the elements in the result is not defined, and it can differ from the order in either input list. To get a predictable order, wrap the call in `list_sort`.

The two list arguments do not handle `NULL` the same way. If `list1` is `NULL`, the result is `NULL`. If only `list2` is `NULL`, the result is an empty list.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-list-list-intersect-differences-from-engine-version-3"></a>

In Athena engine version 3, `array_intersect` keeps a `NULL` element that is in both arrays. In DuckDB, `list_intersect` and its alias `array_intersect` leave `NULL` out of the result.

## `list_position` function
<a name="athena-duckdb-functions-list-list-position"></a>

Searches `list` for `element` and returns the 1-based position of the first element that equals it. If `list` does not contain `element`, the result is `NULL`.

A `NULL` value of `element` matches a `NULL` element in the list, so `list_position([1, NULL, 3], NULL)` returns `2`. If `list` is `NULL`, the result is `NULL`.

`array_indexof`, `array_position`, and `list_indexof` are aliases of `list_position`. To test only whether a list contains an element, use the [`list_contains` function](#athena-duckdb-functions-list-list-contains).

### Syntax
<a name="athena-duckdb-functions-list-list-position-syntax"></a>

```sql
list_position({{list}}, {{element}})
```

### Arguments
<a name="athena-duckdb-functions-list-list-position-arguments"></a>

The `list_position` function takes the following arguments:

**{{list}}**  
The list to search, of any element type.

**{{element}}**  
The value to find, of a type that can be compared with the elements of `list`.

### Return type
<a name="athena-duckdb-functions-list-list-position-return-type"></a>

`INTEGER`

### Examples
<a name="athena-duckdb-functions-list-list-position-examples"></a>

The following SQL query returns the position of the value `2` in a list.

```sql
SELECT list_position([1, 2, NULL], 2) AS result
```

The query returns the following result.


| result | 
| --- | 
| `2` | 

The following SQL query uses the `list_indexof` alias. The value `20` appears twice, and the function returns the position of the first one.

```sql
SELECT list_indexof([10, 20, 30, 20], 20) AS result
```

The query returns the following result.


| result | 
| --- | 
| `2` | 

The following SQL query uses the `array_position` alias to search for a value that the list does not contain, so the result is `NULL`.

```sql
SELECT array_position([5, 6, 7], 9) AS result
```

The query returns the following result.


| result | 
| --- | 
| `NULL` | 

The following SQL query searches for `NULL`, which matches the `NULL` element at position 2.

```sql
SELECT list_position([1, NULL, 3], NULL) AS result
```

The query returns the following result.


| result | 
| --- | 
| `2` | 

### Considerations
<a name="athena-duckdb-functions-list-list-position-considerations"></a>

String comparisons are case-sensitive. For example, `list_position(['a', 'b'], 'B')` returns `NULL`.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-list-list-position-differences-from-engine-version-3"></a>

In Athena engine version 3, `array_position` returns `0` when the array does not contain the element. In DuckDB, `array_position` is an alias of `list_position` and returns `NULL`. To get the Athena engine version 3 result, use `coalesce(list_position({{list}}, {{element}}), 0)`.

In Athena engine version 3, `array_position` returns `NULL` when the element to find is `NULL`. In DuckDB, `list_position` returns the position of the first `NULL` element in the list.

## `list_unique` function
<a name="athena-duckdb-functions-list-list-unique"></a>

Returns how many different values `list` holds. A value that appears more than once is counted once, and `NULL` elements are not counted. For an empty list, or a list of only `NULL` elements, the result is `0`. If `list` is `NULL`, the result is `NULL`.

The result equals `len(list_distinct({{list}}))`. Strings are compared with case, so `'a'` and `'A'` count as two values. `array_unique` is an alias of `list_unique`.

### Syntax
<a name="athena-duckdb-functions-list-list-unique-syntax"></a>

```sql
list_unique({{list}})
```

### Arguments
<a name="athena-duckdb-functions-list-list-unique-arguments"></a>

The `list_unique` function takes the following argument:

**{{list}}**  
The list whose distinct elements you want to count, as a `LIST` value.

### Return type
<a name="athena-duckdb-functions-list-list-unique-return-type"></a>

`UBIGINT`

### Examples
<a name="athena-duckdb-functions-list-list-unique-examples"></a>

The following SQL query counts the distinct values in a list. The value `1` appears three times and is counted once, and the `NULL` element is not counted.

```sql
SELECT list_unique([1, 1, NULL, -3, 1, 5]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `3` | 

The following SQL query shows that strings that differ only in case are counted as different values.

```sql
SELECT list_unique(['a', 'A', 'a']) AS result
```

The query returns the following result.


| result | 
| --- | 
| `2` | 

The following SQL query counts the distinct values in an empty list and in a list of only `NULL` elements. Both results are `0`.

```sql
SELECT
    list_unique([]) AS empty_list,
    list_unique(
        [CAST(NULL AS INTEGER), NULL]
    ) AS only_nulls
```

The query returns the following result.


| empty\_list | only\_nulls | 
| --- | --- | 
| `0` | `0` | 

### Considerations
<a name="athena-duckdb-functions-list-list-unique-considerations"></a>

Hive and Iceberg tables cannot store `UBIGINT` values. To write the result to a table, cast it, as in `CAST(list_unique({{list}}) AS BIGINT)`.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-list-list-unique-differences-from-engine-version-3"></a>

In Athena engine version 3, you count the distinct elements of an array with `cardinality(array_distinct({{array}}))`, which counts `NULL` as one distinct value. The `list_unique` function does not count `NULL`, so for a list that contains `NULL`, its result is 1 lower.