How to Use string_agg in PostgreSQL
Last updated July 25, 2026 · By the SaturnSQL team
string_agg(expression, delimiter) concatenates values from a group into one string, Postgres's equivalent of MySQL's GROUP_CONCAT. Add ORDER BY inside the call to control ordering and DISTINCT to drop repeats.
SELECT customer_id,
string_agg(status, ', ' ORDER BY created_at) AS status_history
FROM orders
GROUP BY customer_id;Distinct values only
SELECT customer_id,
string_agg(DISTINCT status, ', ') AS statuses
FROM orders
GROUP BY customer_id;Non-text columns need a cast, e.g. string_agg(id::text, ','). For an array instead of a string, use array_agg with the same syntax.
Run this in SaturnSQL
SaturnSQL is a browser-based SQL editor for teams: shared query library, schema-aware autocomplete, and scheduled exports to Google Sheets and Slack.
Try it free