9951 explained code solutions for 126 technologies


postgresqlHow do I use the PostgreSQL array_agg function?


The PostgreSQL array_agg function is used to aggregate values from a group of rows into a single array. It takes an expression as an argument and returns an array containing all the values from the group.

Example

SELECT array_agg(name)
FROM users

Output example

{'John', 'Mary', 'James'}

This example returns an array containing the name values from the users table.

Code explanation

  • SELECT - specifies the columns to be selected
  • array_agg - the PostgreSQL array_agg function
  • name - the expression to be aggregated into an array
  • FROM users - specifies the table from which to select the values

Helpful links

Edit this code on GitHub