content/shared/sql-reference/functions/array.md
Use array functions to create and operate on Arrow arrays or lists in SQL queries.
Returns the first non-null element in the array.
array_any_value(array)
list_any_value{{< expand-wrapper >}}
{{% expand "View array_any_value example" %}}
SELECT array_any_value([NULL, 1, 2, 3]) AS array_any_value
| array_any_value |
|---|
| 1 |
{{% /expand %}} {{< /expand-wrapper >}}
Appends an element to the end of an array.
array_append(array, element)
list_appendarray_push_backlist_push_back{{< expand-wrapper >}}
{{% expand "View array_append example" %}}
SELECT array_append([1, 2, 3], 4) AS array_append
| array_append |
|---|
| [1, 2, 3, 4] |
{{% /expand %}} {{< /expand-wrapper >}}
Alias of array_concat.
Concatenates multiple arrays into a single array.
array_concat(array[, ..., array_n])
array_catlist_concatlist_cat{{< expand-wrapper >}}
{{% expand "View array_concat example" %}}
SELECT array_concat([1, 2], [3, 4], [5, 6]) AS array_concat
| array_concat |
|---|
| [1, 2, 3, 4, 5, 6] |
{{% /expand %}} {{< /expand-wrapper >}}
Alias of array_has.
Returns an array of the array's dimensions.
array_dims(array)
list_dims{{< expand-wrapper >}}
{{% expand "View array_dims example" %}}
SELECT array_dims([[1, 2, 3], [4, 5, 6]]) AS array_dims
| array_dims(List([1,2,3,4,5,6])) |
|---|
| [2, 3] |
{{% /expand %}} {{< /expand-wrapper >}}
Returns the Euclidean distance between two input arrays of equal length.
array_distance(array1, array2)
list_distance{{< expand-wrapper >}}
{{% expand "View array_distance example" %}}
SELECT array_distance([1, 2], [1, 4]) AS array_distance
| array_distance |
|---|
| 2.0 |
{{% /expand %}} {{< /expand-wrapper >}}
Returns distinct values from the array after removing duplicates.
array_distinct(array)
list_distinct{{< expand-wrapper >}}
{{% expand "View array_distinct example" %}}
SELECT array_distinct([1, 3, 2, 3, 1, 2, 4]) AS array_distinct
| array_distinct(List([1,2,3,4])) |
|---|
| [1, 2, 3, 4] |
{{% /expand %}} {{< /expand-wrapper >}}
Extracts the element with the index n from the array.
array_element(array, index)
array_extractlist_elementlist_extract{{< expand-wrapper >}}
{{% expand "View array_element example" %}}
SELECT array_element([1, 2, 3, 4], 3) AS array_element
| array_element |
|---|
| 3 |
{{% /expand %}} {{< /expand-wrapper >}}
Alias of empty.
Returns an array containing elements from the first array that are not present in the second array.
array_except(array1, array2)
list_except{{< expand-wrapper >}}
{{% expand "View array_except example" %}}
SELECT array_except([1, 2, 3, 4], [5, 6, 3, 4]) AS array_except
| array_except |
|---|
| [1, 2] |
{{% /expand %}} {{< /expand-wrapper >}}
Alias of array_element.
Returns true if the array contains the element.
array_has(array, element)
list_hasarray_containslist_contains{{< expand-wrapper >}}
{{% expand "View array_has example" %}}
SELECT array_has([1, 2, 3], 2) AS array_has
| array_has |
|---|
| true |
{{% /expand %}} {{< /expand-wrapper >}}
Returns true if all elements of sub-array exist in array.
array_has_all(array, sub-array)
list_has_all{{< expand-wrapper >}}
{{% expand "View array_has_all example" %}}
SELECT array_has_all([1, 2, 3, 4], [2, 3]) AS array_has_all
| array_has_all |
|---|
| true |
{{% /expand %}} {{< /expand-wrapper >}}
Returns true if at least one element appears in both arrays.
array_has_any(array, sub-array)
list_has_anyarrays_overlap{{< expand-wrapper >}}
{{% expand "View array_has_any example" %}}
SELECT array_has_any([1, 2, 3], [3, 4]) AS array_has_any
| array_has_any |
|---|
| true |
{{% /expand %}} {{< /expand-wrapper >}}
Alias of array_position.
Returns an array containing only the elements that appear in both array1 and array2.
array_intersect(array1, array2)
list_intersect{{< expand-wrapper >}}
{{% expand "View array_intersect example with intersecting arrays" %}}
SELECT array_intersect([1, 2, 3, 4], [5, 6, 3, 4]) AS array_intersect
| array_intersect |
|---|
| [3, 4] |
{{% /expand %}}
{{% expand "View array_intersect example with non-intersecting arrays" %}}
SELECT array_intersect([1, 2, 3, 4], [5, 6, 7, 8]) AS array_intersect
| array_intersect |
|---|
| [] |
{{% /expand %}} {{< /expand-wrapper >}}
Alias of array_to_string.
Returns the length of the array dimension.
array_length(array, dimension)
1.list_length{{< expand-wrapper >}}
{{% expand "View array_length example with single-dimension array" %}}
SELECT array_length([1, 2, 3, 4, 5]) AS array_length
| array_length |
|---|
| 5 |
{{% /expand %}}
{{% expand "View array_length example with multi-dimension array" %}}
WITH vars AS (
SELECT [
[1, 2, 3, 4, 5],
[5, 6, 7, 8, 9]
] AS example_array
)
SELECT
array_length(example_array, 1) AS 'dim1_length',
array_length(example_array, 2) AS 'dim2_length'
FROM vars
| dim1_length | dim2_length |
|---|---|
| 2 | 5 |
{{% /expand %}} {{< /expand-wrapper >}}
Returns the maximum value in the array.
array_max(array)
list_max{{< expand-wrapper >}}
{{% expand "View array_max example" %}}
SELECT array_max([3,1,4,2]) AS array_max
| array_max |
|---|
| 4 |
{{% /expand %}} {{< /expand-wrapper >}}
Returns the minimum value in the array.
array_min(array)
{{< expand-wrapper >}}
{{% expand "View array_min example" %}}
SELECT array_min([3,1,4,2]) AS array_min
| array_min |
|---|
| 1 |
{{% /expand %}} {{< /expand-wrapper >}}
Returns the number of dimensions of the array.
array_ndims(array)
list_ndims{{< expand-wrapper >}}
{{% expand "View array_ndims example" %}}
SELECT array_ndims([[1, 2, 3], [4, 5, 6]]) AS array_ndims
| array_ndims |
|---|
| 2 |
{{% /expand %}} {{< /expand-wrapper >}}
Returns the array without the last element.
array_pop_back(array)
list_pop_back{{< expand-wrapper >}}
{{% expand "View array_pop_back example" %}}
SELECT array_pop_back([1, 2, 3]) AS array_pop_back
| array_pop_back |
|---|
| [1, 2] |
{{% /expand %}} {{< /expand-wrapper >}}
Returns the array without the first element.
array_pop_front(array)
list_pop_front{{< expand-wrapper >}}
{{% expand "View array_pop_front example" %}}
SELECT array_pop_front([1, 2, 3]) AS array_pop_front
| array_pop_front |
|---|
| [2, 3] |
{{% /expand %}} {{< /expand-wrapper >}}
Returns the position of the first occurrence of the specified element in the array, or NULL if not found.
array_position(array, element, index)
1.list_positionarray_indexoflist_indexof{{< expand-wrapper >}}
{{% expand "View array_position example" %}}
SELECT array_position([1, 2, 2, 3, 1, 4], 2) AS array_position
| array_position |
|---|
| 2 |
{{% /expand %}}
{{% expand "View array_position example with index offset" %}}
SELECT array_position([1, 2, 2, 3, 1, 4], 2, 3) AS array_position
| array_position |
|---|
| 3 |
{{% /expand %}} {{< /expand-wrapper >}}
Searches for an element in the array and returns the position or index of each occurrence.
array_positions(array, element)
list_positions{{< expand-wrapper >}}
{{% expand "View array_positions example" %}}
SELECT array_positions(['John', 'Jane', 'James', 'John'], 'John') AS array_positions
| array_positions |
|---|
| [1, 4] |
{{% /expand %}} {{< /expand-wrapper >}}
Prepends an element to the beginning of an array.
array_prepend(element, array)
list_prependarray_push_frontlist_push_front{{< expand-wrapper >}}
{{% expand "View array_prepend example" %}}
SELECT array_prepend(1, [2, 3, 4]) AS array_prepend
| array_prepend |
|---|
| [1, 2, 3, 4] |
{{% /expand %}} {{< /expand-wrapper >}}
Alias of array_append.
Alias of array_prepend.
Removes the first element from the array equal to the given value.
array_remove(array, element)
list_remove{{< expand-wrapper >}}
{{% expand "View array_remove example" %}}
SELECT array_remove([1, 2, 2, 3, 2, 1, 4], 2) AS array_remove
| array_remove |
|---|
| [1, 2, 3, 2, 1, 4] |
{{% /expand %}} {{< /expand-wrapper >}}
Removes all elements from the array equal to the specified value.
array_remove_all(array, element)
list_remove_all{{< expand-wrapper >}}
{{% expand "View array_remove_all example" %}}
SELECT array_remove_all([1, 2, 2, 3, 2, 1, 4], 2) AS array_remove_all
| array_remove_all |
|---|
| [1, 3, 1, 4] |
{{% /expand %}} {{< /expand-wrapper >}}
Removes the first max elements from the array equal to the specified value.
array_remove_n(array, element, max)
list_remove_n{{< expand-wrapper >}}
{{% expand "View array_remove_n example" %}}
SELECT array_remove_n([1, 2, 2, 3, 2, 1, 4], 2, 2) AS array_remove_n
| array_remove_n |
|---|
| [1, 3, 2, 1, 4] |
{{% /expand %}} {{< /expand-wrapper >}}
Returns an array containing element count times.
array_repeat(element, count)
list_repeat{{< expand-wrapper >}}
{{% expand "View array_repeat example with numeric values" %}}
SELECT array_repeat(1, 3) AS array_repeat
| array_repeat |
|---|
| [1, 1, 1] |
{{% /expand %}}
{{% expand "View array_repeat example with string values" %}}
SELECT array_repeat('John', 3) AS array_repeat
| array_repeat |
|---|
| [John, John, John] |
{{% /expand %}}
{{% expand "View array_repeat example with array values" %}}
SELECT array_repeat([1, 2], 2) AS array_repeat
| array_repeat |
|---|
| [[1, 2], [1, 2]] |
{{% /expand %}} {{< /expand-wrapper >}}
Replaces the first occurrence of the specified element with another specified element.
array_replace(array, from, to)
list_replace{{< expand-wrapper >}}
{{% expand "View array_replace example" %}}
SELECT array_replace(['John', 'Jane', 'James', 'John'], 'John', 'Joe') AS array_replace
| array_replace |
|---|
| [Joe, Jane, James, John] |
{{% /expand %}} {{< /expand-wrapper >}}
Replaces all occurrences of the specified element with another specified element.
array_replace_all(array, from, to)
list_replace_all{{< expand-wrapper >}}
{{% expand "View array_replace_all example" %}}
SELECT array_replace_all(['John', 'Jane', 'James', 'John'], 'John', 'Joe') AS array_replace_all
| array_replace_all |
|---|
| [Joe, Jane, James, Joe] |
{{% /expand %}} {{< /expand-wrapper >}}
Replaces the first max occurrences of the specified element with another
specified element.
array_replace_n(array, from, to, max)
list_replace_n{{< expand-wrapper >}}
{{% expand "View array_replace_n example" %}}
SELECT array_replace_n(['John', 'Jane', 'James', 'John', 'John'], 'John', 'Joe', 2) AS array_replace_n
| array_replace_n |
|---|
| [Joe, Jane, James, Joe, John] |
{{% /expand %}} {{< /expand-wrapper >}}
Resizes the list to contain size elements. Initializes new elements with value Resizes the array to the specified size. If expanding, fills new elements with the specified value (or NULL if not provided). If shrinking, truncates excess elements.
array_resize(array, size, value)
list_resize{{< expand-wrapper >}}
{{% expand "View array_resize example" %}}
SELECT array_resize([1, 2, 3], 5, 0) AS array_resize
| array_resize(List([1,2,3],5,0)) |
|---|
| [1, 2, 3, 0, 0] |
{{% /expand %}} {{< /expand-wrapper >}}
Returns the array with the order of the elements reversed.
array_reverse(array)
list_reverse{{< expand-wrapper >}}
{{% expand "View array_reverse example" %}}
SELECT array_reverse([1, 2, 3, 4]) AS array_reverse
| array_reverse |
|---|
| [4, 3, 2, 1] |
{{% /expand %}} {{< /expand-wrapper >}}
Returns a slice of the array based on 1-indexed start and end positions.
array_slice(array, begin, end)
1.list_slice{{< expand-wrapper >}}
{{% expand "View array_slice example" %}}
SELECT array_slice([1, 2, 3, 4, 5, 6, 7, 8], 3, 6) AS array_slice
| array_slice |
|---|
| [3, 4, 5, 6] |
{{% /expand %}} {{< /expand-wrapper >}}
Sorts elements in an array. If elements are numeric, it sorts elements in numerical order. If elements are strings, it sorts elements in lexicographical order.
array_sort(array, sort_order, sort_nulls)
'ASC' (default) or 'DESC').'NULLS FIRST' (default) or 'NULLS LAST').list_sort{{< expand-wrapper >}}
{{% expand "View array_sort example with numeric elements" %}}
SELECT array_sort([3, 1, 2]) AS array_sort
| array_sort |
|---|
| [1, 2, 3] |
{{% /expand %}}
{{% expand "View array_sort example with string elements" %}}
SELECT array_sort(['banana', 'apple', 'cherry'], 'DESC') AS array_sort
| array_sort |
|---|
| [cherry, banana, apple] |
{{% /expand %}}
{{% expand "View array_sort example with NULL elements" %}}
SELECT
array_sort(
['banana', 'apple', NULL, 'cherry', NULL],
'ASC',
'NULLS LAST'
) AS array_sort
| array_sort |
|---|
| [apple, banana, cherry, , ] |
{{% /expand %}} {{< /expand-wrapper >}}
Converts an array to a string by joining all elements with the specified delimiter.
array_to_string(array, delimiter[, null_string])
list_to_stringarray_joinlist_join{{< expand-wrapper >}}
{{% expand "View array_to_string example" %}}
SELECT array_to_string([1,2,3,4,5,6,7,8], ',') AS array_to_string
| array_to_string |
|---|
| 1,2,3,4,5,6,7,8 |
{{% /expand %}}
{{% expand "View array_to_string example with NULL replacements" %}}
SELECT array_to_string([[1,2,3,4,5,NULL,7,8,NULL]], '-', '?') AS array_to_string
| array_to_string |
|---|
| 1-2-3-4-5-?-7-8-? |
{{% /expand %}} {{< /expand-wrapper >}}
Returns an array of elements that are present in both arrays (all elements from Returns an array containing all unique elements from both input arrays, with duplicates removed.
array_union(array1, array2)
list_union{{< expand-wrapper >}}
{{% expand "View array_union example" %}}
SELECT array_union([1, 2, 3, 4], [5, 6, 3, 4]) AS array_union
| array_union |
|---|
| [1, 2, 3, 4, 5, 6] |
{{% /expand %}} {{< /expand-wrapper >}}
Alias of array_has_any.
Returns the total number of elements in the array.
cardinality(array)
{{< expand-wrapper >}}
{{% expand "View cardinality example" %}}
SELECT cardinality([[1, 2, 3, 4], [5, 6, 7, 8]]) AS cardinality
| cardinality |
|---|
| 8 |
{{% /expand %}} {{< /expand-wrapper >}}
Returns true for an empty array or false for a non-empty array.
empty(array)
array_emptylist_empty{{< expand-wrapper >}}
{{% expand "View empty example" %}}
SELECT empty(['apple']) AS empty
| empty |
|---|
| false |
{{% /expand %}} {{< /expand-wrapper >}}
Flattens nested arrays into a single-level array.
The result contains all elements from all nested arrays in a single flat array.
flatten(array)
{{< expand-wrapper >}}
{{% expand "View flatten example" %}}
SELECT flatten([[1, 2], [3, 4]]) AS flattened
| flattened |
|---|
| [1, 2, 3, 4] |
{{% /expand %}} {{< /expand-wrapper >}}
Returns an array with values between the specified start and stop values generated at the specified step.
The range start..stop contains all values greater than or equal to start
and less than or equal to stop (start <= x <= stop).
If start is greater than or equal to stop (start >= stop), the
function returns an empty array.
generate_series is similar to range, but includes the upper bound
(stop) in the output array.
generate_series(start, stop, step)
Date32.Date32. The type must be the
same as start.0). Steps less than a day are
only supported for ranges with the TIMESTAMP type.{{< expand-wrapper >}}
{{% expand "View generate_series example" %}}
SELECT generate_series(1,5) AS generate_series
| generate_series |
|---|
| [1, 2, 3, 4, 5] |
{{% /expand %}}
{{% expand "View range example with dates" %}}
SELECT
generate_series(
DATE '2025-03-01',
DATE '2025-08-01',
INTERVAL '1 month'
) AS generate_series
| generate_series |
|---|
| [2025-03-01, 2025-04-01, 2025-05-01, 2025-06-01, 2025-07-01, 2025-08-01] |
{{% /expand %}}
{{% expand "View generate_series example using timestamps" %}}
SELECT
generate_series(
'2025-01-01T00:00:00Z'::timestamp,
'2025-01-01T06:00:00Z'::timestamp,
INTERVAL '2 hours'
) AS generate_series
| generate_series |
|---|
| [2025-01-01T00:00:00, 2025-01-01T02:00:00, 2025-01-01T04:00:00, 2025-01-01T06:00:00] |
{{% /expand %}} {{< /expand-wrapper >}}
Alias of array_any_value.
Alias of array_append.
Alias of array_concat.
Alias of array_concat.
Alias of array_has.
Alias of array_dims.
Alias of array_distance.
Alias of array_distinct.
Alias of array_element.
Alias of empty.
Alias of array_except.
Alias of array_element.
Alias of array_has.
Alias of array_has_all.
Alias of array_has_any.
Alias of array_position.
Alias of array_intersect.
Alias of array_to_string.
Alias of array_length.
Alias of array_max.
Alias of array_ndims.
Alias of array_pop_back.
Alias of array_pop_front.
Alias of array_position.
Alias of array_positions.
Alias of array_prepend.
Alias of array_append.
Alias of array_prepend.
Alias of array_remove.
Alias of array_remove_all.
Alias of array_remove_n.
Alias of array_repeat.
Alias of array_replace.
Alias of array_replace_all.
Alias of array_replace_n.
Alias of array_resize.
Alias of array_reverse.
Alias of array_slice.
Alias of array_sort.
Alias of array_to_string.
Alias of array_union.
Returns an array using the specified input expressions.
make_array(expression1[, ..., expression_n])
make_list{{< expand-wrapper >}}
{{% expand "View make_array example" %}}
SELECT make_array(1, 2, 3, 4, 5) AS make_array
| make_array |
|---|
| [1, 2, 3, 4, 5] |
{{% /expand %}} {{< /expand-wrapper >}}
Alias of make_array.
Returns an array with values between the specified start and stop values generated at the specified step.
The range start..stop contains all values greater than or equal to start
and less than stop (start <= x < stop).
If start is greater than or equal to stop (start >= stop), the
function returns an empty array.
range is similar to generate_series, but does not include
the upper bound (stop) in the output array.
range(start, stop, step)
Date32.Date32. The type must be the
same as start.0). Steps less than a day are
only supported for ranges with the TIMESTAMP type.{{< expand-wrapper >}}
{{% expand "View range example" %}}
SELECT range(1, 5, 1) AS range
| range |
|---|
| [1, 2, 3, 4] |
{{% /expand %}}
{{% expand "View range example with dates" %}}
SELECT
range(
DATE '2025-03-01',
DATE '2025-08-01',
INTERVAL '1 month'
) AS range
| range |
|---|
| [2025-03-01, 2025-04-01, 2025-05-01, 2025-06-01, 2025-07-01] |
{{% /expand %}} {{% expand "View range example with timestamps" %}}
SELECT
range(
'2025-01-01T00:00:00Z'::timestamp,
'2025-01-01T06:00:00Z'::timestamp,
INTERVAL '2 hours'
) AS range
| range |
|---|
| [2025-01-01T00:00:00, 2025-01-01T02:00:00, 2025-01-01T04:00:00] |
{{% /expand %}} {{< /expand-wrapper >}}
Splits a string into an array of substrings based on a delimiter. Any substrings
matching the optional null_str argument are replaced with NULL.
string_to_array(str, delimiter[, null_str])
NULL.string_to_list{{< expand-wrapper >}}
{{% expand "View string_to_array example with comma-delimited list" %}}
SELECT string_to_array('abc, def, ghi', ', ') AS string_to_array
| string_to_array |
|---|
| [abc, def, ghi] |
{{% /expand %}}
{{% expand "View string_to_array example with a non-standard delimiter" %}}
SELECT string_to_array('abc##def', '##') AS string_to_array
| string_to_array |
|---|
| ['abc', 'def'] |
{{% /expand %}}
{{% expand "View string_to_array example with NULL replacements" %}}
SELECT string_to_array('abc def', ' ', 'def') AS string_to_array
| string_to_array |
|---|
| ['abc', NULL] |
{{% /expand %}} {{< /expand-wrapper >}}
Alias of string_to_array.