Skip to main content
Concatenates input values, including NULL values, into an array.

Syntax

ARRAY_AGG(<expression>) [FILTER ([WHERE] <condition>)]

Parameters

ParameterDescriptionSupported input type
<expression>Expression of any type to be accumulated into an array.Any
<condition>An optional boolean expression to filter rows used in aggregationBOOL

Return Type

ARRAY of the same type as the input data. If there is no input data, ARRAY_AGG returns NULL.

Example

The following examples use a player_information table:
SELECT nickname, playerid
FROM (VALUES
    ('stephen70', 1),
    ('burchdenise', 7),
    ('sabrina21', NULL)
) AS player_information(nickname, playerid);
nickname textplayerid int null
stephen701
burchdenise7
sabrina21NULL

Rows: 3Execution time: 5ms

The following example aggregates the columns into arrays, including NULL values:
SELECT
    ARRAY_AGG(nickname) AS nicknames,
    ARRAY_AGG(playerid) AS playerids
FROM
    (VALUES
        ('stephen70', 1),
        ('burchdenise', 7),
        ('sabrina21', NULL)
    ) AS t(nickname, playerid);
nicknames array(text) nullplayerids array(int null) null
[‘stephen70’, ‘burchdenise’, ‘sabrina21’][1, 7, NULL]

Rows: 1Execution time: 10.40ms

The following example shows that when a WHERE filter rejects all rows, ARRAY_AGG returns NULL:
SELECT
    ARRAY_AGG(nickname) AS nicknames,
    ARRAY_AGG(playerid) AS playerids
FROM
    (VALUES
        ('stephen70', 1),
        ('burchdenise', 7),
        ('sabrina21', NULL)
    ) AS t(nickname, playerid)
WHERE
    playerid = 42;
nicknames array(text) nullplayerids array(int) null
NULLNULL

Rows: 1Execution time: 8.85ms