Skip to content

ARRAY_AGG

  • expression: This is the specific column or set of data within the table that the ARRAY_AGG function is to be applied on. The function aggregates the data from the specified column into a single PostgreSQL array.
SELECT department, ARRAY_AGG(employee_name)
FROM company
GROUP BY department;
department | array_agg
------------+-----------------------
finance | {John, Jane, James}
sales | {Smith, Peter, Paul}

In this example, the ARRAY_AGG function in PostgreSQL is used to concatenate the employee names into an array for each unique department in the “company” table. For each department, you are presented with an array of employee names associated with that department.