How to Use LISTAGG in Redshift
Last updated July 25, 2026 · By the SaturnSQL team
LISTAGG(column, delimiter) WITHIN GROUP (ORDER BY ...) concatenates values from a group into one string. The result is limited to 65,535 bytes; the query fails if a group exceeds it.
WITHIN GROUP (ORDER BY ...) is what makes the output deterministic. Without it the order is whatever the query happens to produce and can change between runs, turning a stable report into a noisy diff. DISTINCT deduplicates before concatenating, and the delimiter argument is required since there is no default.
SELECT customer_id,
LISTAGG(DISTINCT status, ', ') WITHIN GROUP (ORDER BY status) AS statuses
FROM orders
GROUP BY customer_id;As a window function
As a window function, LISTAGG keeps every input row and attaches the full aggregated list to each one rather than collapsing the group. Note that it takes OVER (PARTITION BY ...) but no ORDER BY inside OVER, because the ordering belongs in WITHIN GROUP. The 65,535 byte cap applies here too and raises an error rather than truncating.
SELECT id, customer_id,
LISTAGG(id, ',') WITHIN GROUP (ORDER BY created_at)
OVER (PARTITION BY customer_id) AS all_order_ids
FROM orders;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