Array functions

**Using the ARRAY JOIN syntax**

```
-- Note the ARRAY JOIN syntax in the `SELECT` query below, which provides broader possibilities.
-- ARRAY JOIN allows you to convert multiple arrays with the same number of elements at a time.

SELECT
    sum(1) AS impressions,
    city,
    browser
FROM
(
    SELECT
        ['Istanbul', 'Berlin', 'Bobruisk'] AS cities,
        ['Firefox', 'Chrome', 'Chrome'] AS browsers
)
ARRAY JOIN
    cities AS city,
    browsers AS browser
GROUP BY
    2,
    3
```

```
┌─impressions─┬─city─────┬─browser─┐
│           1 │ Istanbul │ Firefox │
│           1 │ Berlin   │ Chrome  │
│           1 │ Bobruisk │ Chrome  │
└─────────────┴──────────┴─────────┘
```

**Using Tuple**

```
-- You can also use Tuple

SELECT
    sum(1) AS impressions,
    (arrayJoin(arrayZip(cities, browsers)) AS t).1 AS city,
    t.2 AS browser
FROM
(
    SELECT
        ['Istanbul', 'Berlin', 'Bobruisk'] AS cities,
        ['Firefox', 'Chrome', 'Chrome'] AS browsers
)
GROUP BY
    2,
    3
```

```
┌─impressions─┬─city─────┬─browser─┐
│           1 │ Istanbul │ Firefox │
│           1 │ Berlin   │ Chrome  │
│           1 │ Bobruisk │ Chrome  │
└─────────────┴──────────┴─────────┘
```

## arrayLast[​](#arrayLast "Direct link to arrayLast")

Introduced in: v1.1.0

Returns the last element in the source array for which a lambda `func(x [, y1, y2, ... yN])` returns true, otherwise it returns a default value.

**Syntax**

```
arrayLast(func(x[, y1, ..., yN]), source[, cond1, ... , condN_arr])
```

**Arguments**

- `func(x[, y1, ..., yN])` — A lambda function which operates on elements of the source array (`x`) and condition arrays (`y`). [Lambda function](/docs/sql-reference/functions/overview#arrow-operator-and-lambda). - `source` — The source array to process. [`Array(T)`](/docs/sql-reference/data-types/array). - `[, cond1, ... , condN]` — Optional. N condition arrays providing additional arguments to the lambda function. [`Array(T)`](/docs/sql-reference/data-types/array).

**Returned value**

Returns the last element of the source array for which `func` is true, otherwise returns the default value of `T`.

**Examples**

**Usage example**

```
SELECT arrayLast(x, y -> x=y, ['a', 'b', 'c'], ['a', 'b', 'c'])
```

```
c
```

**No match**

```
SELECT arrayFirst(x, y -> x=y, [0, 1, 2], [3, 3, 3]) AS res, toTypeName(res)
```

```
0 UInt8
```

## arrayLastIndex[​](#arrayLastIndex "Direct link to arrayLastIndex")

Introduced in: v1.1.0

Returns the index of the last element in the source array for which `func(x[, y1, y2, ... yN])` returns true, otherwise it returns '0'.

**Syntax**

```
arrayLastIndex(func(x[, y1, ..., yN]), source_arr[, cond1_arr, ... , condN_arr])
```

**Arguments**

- `func(x[, y1, ..., yN])` — A lambda function which operates on elements of the source array (`x`) and condition arrays (`y`). [`Lambda function`](/docs/sql-reference/functions/overview#arrow-operator-and-lambda)
- `source_arr` — The source array to process. [`Array(T)`](/docs/sql-reference/data-types/array)
- `[, cond1_arr, ... , condN_arr]` — Optional. N condition arrays providing additional arguments to the lambda function. [`Array(T)`](/docs/sql-reference/data-types/array)

**Returned value**

Returns the index of the last element of the source array for which `func` is true, otherwise returns `0` [`UInt32`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT arrayLastIndex(x, y -> x=y, ['a', 'b', 'c'], ['a', 'b', 'c']);
```

```
3
```

**No match**

```
SELECT arrayLastIndex(x, y -> x=y, ['a', 'b', 'c'], ['d', 'e', 'f']);
```

```
0
```

## arrayLastOrNull[​](#arrayLastOrNull "Direct link to arrayLastOrNull")

Introduced in: v1.1.0

Returns the last element in the source array for which a lambda `func(x [, y1, y2, ... yN])` returns true, otherwise it returns `NULL`.

**Syntax**

```
arrayLastOrNull(func(x[, y1, ..., yN]), source_arr[, cond1_arr, ... , condN_arr])
```

**Arguments**

- `func(x [, y1, ..., yN])` — A lambda function which operates on elements of the source array (`x`) and condition arrays (`y`). [Lambda function](/docs/sql-reference/functions/overview#arrow-operator-and-lambda). - `source_arr` — The source array to process. [`Array(T)`](/docs/sql-reference/data-types/array). - `[, cond1_arr, ... , condN_arr]` — Optional. N condition arrays providing additional arguments to the lambda function. [`Array(T)`](/docs/sql-reference/data-types/array).

**Returned value**

Returns the last element of the source array for which `λ` is not true, otherwise returns `NULL`.

**Examples**

**Usage example**

```
SELECT arrayLastOrNull(x, y -> x=y, ['a', 'b', 'c'], ['a', 'b', 'c'])
```

```
c
```

**No match**

```
SELECT arrayLastOrNull(x, y -> x=y, [0, 1, 2], [3, 3, 3]) AS res, toTypeName(res)
```

```
NULL Nullable(UInt8)
```

## arrayLevenshteinDistance[​](#arrayLevenshteinDistance "Direct link to arrayLevenshteinDistance")

Introduced in: v25.4.0

Calculates the Levenshtein distance for two arrays.

**Syntax**

```
arrayLevenshteinDistance(from, to)
```

**Arguments**

- `from` — The first array. [`Array(T)`](/docs/sql-reference/data-types/array). - `to` — The second array. [`Array(T)`](/docs/sql-reference/data-types/array).

**Returned value**

Levenshtein distance between the first and the second arrays. [`Float64`](/docs/sql-reference/data-types/float)

**Examples**

**Usage example**

```
SELECT arrayLevenshteinDistance([1, 2, 4], [1, 2, 3])
```

```
1
```

## arrayLevenshteinDistanceWeighted[​](#arrayLevenshteinDistanceWeighted "Direct link to arrayLevenshteinDistanceWeighted")

Introduced in: v25.4.0

Calculates Levenshtein distance for two arrays with custom weights for each element.
The number of elements for the array and its weights should match.

**Syntax**

```
arrayLevenshteinDistanceWeighted(from, to, from_weights, to_weights)
```

**Arguments**

- `from` — first array. [`Array(T)`](/docs/sql-reference/data-types/array). - `to` — second array. [`Array(T)`](/docs/sql-reference/data-types/array). - `from_weights` — weights for the first array. [`Array((U)Int*|Float*)`](/docs/sql-reference/data-types/array)
- `to_weights` — weights for the second array. [`Array((U)Int*|Float*)`](/docs/sql-reference/data-types/array)

**Returned value**

Levenshtein distance between the first and the second arrays with custom weights for each element [`Float64`](/docs/sql-reference/data-types/float)

**Examples**

**Usage example**

```
SELECT arrayLevenshteinDistanceWeighted(['A', 'B', 'C'], ['A', 'K', 'L'], [1.0, 2, 3], [3.0, 4, 5])
```

```
14
```

## arrayMap[​](#arrayMap "Direct link to arrayMap")

Introduced in: v1.1.0

Returns an array obtained from the original arrays by applying a lambda function to each element.

**Syntax**

```
arrayMap(func, arr)
```

**Arguments**

- `func` — A lambda function which operates on elements of the source array (`x`) and condition arrays (`y`). [`Lambda function`](/docs/sql-reference/functions/overview#arrow-operator-and-lambda)
- `arr` — N arrays to process. [`Array(T)`](/docs/sql-reference/data-types/array)

**Returned value**

Returns an array from the lambda results [`Array(T)`](/docs/sql-reference/data-types/array)

**Examples**

**Usage example**

```
SELECT arrayMap(x -> (x + 2), [1, 2, 3]) as res;
```

```
[3, 4, 5]
```

**Creating a tuple of elements from different arrays**

```
SELECT arrayMap((x, y) -> (x, y), [1, 2, 3], [4, 5, 6]) AS res
```

```
[(1, 4),(2, 5),(3, 6)]
```

## arrayMax[​](#arrayMax "Direct link to arrayMax")

Introduced in: v21.1.0

Returns the maximum element in the source array.

If a lambda function `func` is specified, returns the maximum element of the lambda results.

**Syntax**

```
arrayMax([func(x[, y1, ..., yN])], source_arr[, cond1_arr, ... , condN_arr])
```

**Arguments**