PostgreSQL ARRAY_AGG with ORDER BY
PostgreSQL ARRAY_AGG with ORDER BY is a SQL function in the PostgreSQL Specific category. PostgreSQL: aggregates values into an array, optionally ordered. Can also filter with FILTER clause. The syntax is ARRAY_AGG(column ORDER BY sort_col). It accepts column ORDER BY sort_col. It returns array aggregate. A typical example: SELECT department, ARRAY_AGG(name ORDER BY salary DESC) AS employees_by_salary FROM employees GROUP BY department; -- IT: {"Charlie","Alice","Bob"} (sorted by salary descending) -- With FILTER: SELECT department, ARRAY_AGG(name) FILTER (WHERE salary > 80000) AS high_earners FROM employees GROUP BY department; -- Multi-level: SELECT department, ARRAY_AGG(name ORDER BY hire_date) AS by_hire_date, COUNT(*) AS total FROM employees GROUP BY department; A close relative is SERIAL, which postgreSQL auto-increment type. Creates an INTEGER column with a sequence. Equivalent to AUTO_INCREMENT in MySQL. A close relative is RETURNING, which postgreSQL extension that returns values from INSERT/UPDATE/DELETE statements (like OUTPUT in SQL Server). A close relative is ARRAY, which postgreSQL supports array columns and array operations. Can be multi-dimensional.