How to Use groupArray in ClickHouse
Last updated July 25, 2026 · By the SaturnSQL team
groupArray collects grouped values into an array, preserving one element per row; groupUniqArray keeps only distinct values. Pass a size parameter like groupArray(10)(...) to cap the array length per group.
SELECT user_id,
groupArray(event_type) AS all_events,
groupUniqArray(event_type) AS distinct_events
FROM events
GROUP BY user_id;Cap the array size
SELECT user_id, groupArray(10)(url) AS first_urls
FROM events
GROUP BY user_id;groupArray's element order follows the order rows are processed, which is not guaranteed. Sort explicitly, e.g. with arraySort or by aggregating tuples of (event_time, url).
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