array_agg aggregate function

Returns an array consisting of all values in expr within the group.

Since: Databricks Runtime 10.4


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


  • expr: An expression of any type.

  • cond: An optional boolean expression filtering the rows used for aggregation.


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

This function is a synonym for collect_list


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