---
title: element_at function
description: Learn the syntax of the element\_at function of the SQL language in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/functions/element_at
---

# `element_at` function

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime

Returns the element of an `arrayExpr` at `index`.

Returns the value of `mapExpr` for `key`.

## Syntax

```
element_at(arrayExpr, index)
```

```
element_at(mapExpr, key)
```

## Arguments

* `arrayExpr`: An ARRAY expression.
* `index`: An INTEGER expression.
* `mapExpr`: A MAP expression.
* `key`: An expression matching the type of the keys of `mapExpr`

## Returns

If the first argument is an ARRAY:

* The result is of the type of the elements of `expr`.
* abs(index) must be between 1 and the length of the array.
* If `index` is negative the function accesses elements from the last to the first.
* Databricks raises [INVALID\_INDEX\_OF\_ZERO](https://docs.databricks.com/aws/en/error-messages/error-classes.md#invalid_index_of_zero) if `index` is 0.
* Databricks raises [INVALID\_ARRAY\_INDEX\_IN\_ELEMENT\_AT](https://docs.databricks.com/aws/en/error-messages/invalid-array-index-in-element-at-error-class.md) if `abs(index)` exceeds the length of the array.

If the first argument is a MAP and `key` cannot be matched to an entry in `mapExpr` the function returns null.

> [!NOTE]
>
> In Databricks Runtime, if `spark.sql.ansi.failOnElementNotExists` is `false` the function returns `NULL` instead of raising errors.

## Common error conditions

* [INVALID\_ARRAY\_INDEX\_IN\_ELEMENT\_AT](https://docs.databricks.com/aws/en/error-messages/invalid-array-index-in-element-at-error-class.md)
* [INVALID\_INDEX\_OF\_ZERO](https://docs.databricks.com/aws/en/error-messages/error-classes.md#invalid_index_of_zero)

## Examples

```sql
> SELECT element_at(array(1, 2, 3), 2);
 2

> SELECT try_element_at(array(1, 2, 3), 5);
 NULL

> SELECT element_at(array(1, 2, 3), 0);
 Error: INVALID_INDEX_OF_ZERO

> SELECT element_at(array(1, 2, 3), 5);
 Error: INVALID_ARRAY_INDEX_IN_ELEMENT_AT

> SELECT element_at(map(1, 'a', 2, 'b'), 2);
 b

> SELECT element_at(map(1, 'a', 2, 'b'), 3);
 NULL

> SELECT try_element_at(map(1, 'a', 2, 'b'), 3);
 NULL
```

## Related functions

* [`array_contains` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/array_contains.md)
* [`array_position` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/array_position.md)
* [`try_element_at` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_element_at.md)
