aggregate
ARRAY_AGG
Collects scalar inputs into an array, preserving NULLs and duplicates. Order must be specified when meaningful. Empty input gives NULL rather than an empty array. The array-input overload has additional dimensionality restrictions. Example target: PostgreSQL 17; inline synthetic data; no extensions.
Formal Syntax & Structure
sql syntaxsql
ARRAY_AGG(expression ORDER BY sort_key)Working Example
Example Snippetsql
WITH sample(id,x,y) AS (VALUES (1,1,2),(2,2,4),(3,3,6),(4,4,8),(5,NULL,4)) SELECT array_agg(x ORDER BY id) FROM sample;