Array Functions
Turso Extension: Array types and functions are a Turso-specific feature for working with ordered collections directly in SQL. These functions are not part of the SQLite standard. Array columns require STRICT tables.
Array Construction
ARRAY[] Literal
Constructs an array from a list of expressions.string_to_array
Splits a string into an array using a delimiter.| Parameter | Type | Description |
|---|---|---|
text | TEXT | The string to split |
delimiter | TEXT | The delimiter to split on. If NULL, splits into individual characters |
null_string | TEXT | Optional. Elements matching this string are replaced with NULL |
Element Access
Subscript Operator []
Access individual elements using zero-based indexing. Returns NULL for out-of-bounds or negative indices.Slice Operator [start:end]
Extract a sub-array using half-open range[start, end).
Scalar Functions
array_length
Returns the number of elements in an array.| Parameter | Type | Description |
|---|---|---|
array | BLOB/TEXT | The array to measure |
dimension | INTEGER | Optional. Currently only dimension 1 is supported |
array_append
Appends an element to the end of an array.| Parameter | Type | Description |
|---|---|---|
array | BLOB/TEXT | The array to append to. If NULL, a new single-element array is created |
element | any | The element to append |
array_prepend
Prepends an element to the beginning of an array.| Parameter | Type | Description |
|---|---|---|
element | any | The element to prepend |
array | BLOB/TEXT | The array to prepend to |
array_cat
Concatenates two arrays.| Parameter | Type | Description |
|---|---|---|
array1 | BLOB/TEXT | The first array |
array2 | BLOB/TEXT | The second array |
|| operator also works for array concatenation:
array_remove
Removes all occurrences of an element from an array.| Parameter | Type | Description |
|---|---|---|
array | BLOB/TEXT | The array to remove from |
element | any | The element to remove |
array_contains
Tests whether an array contains a specific element.| Parameter | Type | Description |
|---|---|---|
array | BLOB/TEXT | The array to search |
element | any | The element to search for |
array_position
Returns the zero-based index of the first occurrence of an element.| Parameter | Type | Description |
|---|---|---|
array | BLOB/TEXT | The array to search |
element | any | The element to find |
array_slice
Extracts a sub-array by index range.| Parameter | Type | Description |
|---|---|---|
array | BLOB/TEXT | The source array |
start | INTEGER | Start index (zero-based, inclusive). NULL means 0 |
end | INTEGER | End index (exclusive) |
start to end - 1.
array_to_string
Joins array elements into a string with a delimiter.| Parameter | Type | Description |
|---|---|---|
array | BLOB/TEXT | The array to join |
delimiter | TEXT | The separator between elements |
null_string | TEXT | Optional. Replacement text for NULL elements. If omitted, NULLs are skipped |
array_contains_all
Tests whether one array contains all elements of another.| Parameter | Type | Description |
|---|---|---|
haystack | BLOB/TEXT | The array to search in |
needles | BLOB/TEXT | The array of elements to search for |
needles are found in haystack, 0 otherwise.
@> operator is an alias for this function. See Array Operators.
array_overlap
Tests whether two arrays share any common elements.| Parameter | Type | Description |
|---|---|---|
array1 | BLOB/TEXT | The first array |
array2 | BLOB/TEXT | The second array |
array_overlaps is accepted as an alias. The && operator is also an alias. See Array Operators.
Aggregate Function
array_agg
Collects values from a group of rows into an array.| Parameter | Type | Description |
|---|---|---|
expression | any | The value to collect. NULL values are included |
| grp | array_agg(val) |
|---|---|
| x | [“a”,“b”] |
| y | [“c”,“d”,“e”] |
Array Operators
Containment: @>
Tests whether the left array contains all elements of the right array. Equivalent toarray_contains_all(left, right).
Overlap: &&
Tests whether two arrays share any common elements. Equivalent toarray_overlap(left, right).
Comparison: =, !=, <, >, <=, >=
Arrays support element-wise comparison. Elements are compared pairwise from left to right; if all compared elements are equal, the shorter array is considered less than the longer one.Concatenation: ||
When either operand is an array,|| performs array concatenation or element append/prepend instead of string concatenation.
Using operators in WHERE clauses
Subscript Assignment
Array elements can be updated individually using subscript syntax in UPDATE statements.Examples
Tagging system
Multi-dimensional arrays
Type checking
Array columns in STRICT tables validate element types on insert and update:See Also
- Data Types for array column type declarations
- CREATE TABLE for STRICT table requirements
- Expressions for array operator syntax
- Aggregate Functions for
array_agg