---
title: try_element_at function
description: Learn the syntax of the try\_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/try_element_at
---

# `try_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 10.4 LTS and above

Returns the element of an `arrayExpr` at `index`, or NULL if `index` is out of bound.

Returns the value of `mapExpr` for `key`, or NULL id `key` does not exist.

## Syntax

```
try_element_at(arrayExpr, index)
```

```
try_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`.
* 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.
* The function returns `NULL` if `abs(index)` exceeds the length of the array, or if `key` does not exist in the map.

## Common error conditions

* [INVALID\_INDEX\_OF\_ZERO](https://docs.databricks.com/aws/en/error-messages/error-classes.md#invalid_index_of_zero)

## Examples

```sql
> SELECT try_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), 5);
 Error: INVALID_ARRAY_INDEX_IN_ELEMENT_AT

> SELECT try_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)
* [`element_at` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/element_at.md)
