---
title: array_agg aggregate function
description: Learn the syntax of the array\_agg function of the SQL language in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/gcp/en/sql/language-manual/functions/array_agg
---

# `array_agg` aggregate function

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

Returns an array consisting of all values in `expr` within the group. This function is a synonym for [`collect_list` aggregate function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/collect_list.md).

## Syntax

```
array_agg ( [ALL | DISTINCT] expr ) [FILTER ( WHERE cond ) ]
```

This function can also be invoked as a [window function](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-window-functions.md) using the `OVER` clause.

## Arguments

* `expr`: An expression of any type.
* `cond`: An optional boolean expression filtering the rows used for aggregation.

## Returns

An ARRAY of the argument type.

The order of elements in the array is non-deterministic.
`NULL` values are excluded.

If `DISTINCT` is specified the function collects only unique values and is a synonym for [`collect_set` aggregate function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/collect_set.md).

## Examples

```sql
> SELECT array_agg(col) FROM VALUES (1), (2), (NULL), (1) AS tab(col);
 [1,2,1]
> SELECT array_agg(DISTINCT col) FROM VALUES (1), (2), (NULL), (1) AS tab(col);
 [1,2]
```

## Related functions

* [`array` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/array.md)
* [`collect_list` aggregate function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/collect_list.md)
* [`collect_set` aggregate function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/collect_set.md)
* [Window functions](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-window-functions.md)
