BigQuery · Aggregate function
BigQuery STRING_AGG
STRING_AGG concatenates the non-NULL strings of a group into one value, separated by a delimiter (a comma by default). It is BigQuery's GROUP_CONCAT or LISTAGG: put ORDER BY inside the call to sort the list, DISTINCT to drop duplicates and LIMIT to cap its length.
STRING_AGG builds one STRING (or BYTES) value per group from the values of its rows. NULLs are skipped, the delimiter defaults to , with no space, and a group without any non-NULL value returns NULL rather than an empty string. The order of the pieces is arbitrary unless you add ORDER BY inside the call: the query's own ORDER BY sorts the result rows, not the list inside each row.
Reach for it when porting GROUP_CONCAT from MySQL or legacy BigQuery SQL, LISTAGG from Snowflake or Redshift, or STRING_AGG ... WITHIN GROUP from SQL Server. None of those spellings work in GoogleSQL: GROUP_CONCAT and LISTAGG fail with Function not found, and WITHIN GROUP is a syntax error. The ordering goes inside the parentheses, after the delimiter: STRING_AGG(sku, ', ' ORDER BY line_no).
DISTINCT, ORDER BY and LIMIT can be combined, with one restriction: with DISTINCT, ORDER BY may only sort by the aggregated expression itself. As a window function (with OVER) none of the three is allowed. The whole string of a group lives in one row, and BigQuery's maximum row size is about 100 MB, so cap lists from unbounded groups with LIMIT.
Syntax
STRING_AGG([DISTINCT] expression [, delimiter] [WHERE where_expression] [ORDER BY key [{ASC | DESC}] [, ...]] [LIMIT n]) [OVER over_clause]Arguments and return value
| Argument | Description |
|---|---|
expression | STRING or BYTES. NULLs are skipped. Cast other types first, for example CAST(order_id AS STRING). |
delimiter | Optional separator of the same type as expression. Defaults to , without a space. |
DISTINCT | Keeps each distinct value once. |
ORDER BY key | Sorts the values inside the result. Several keys and ASC or DESC are allowed. With DISTINCT, the key must be expression itself. |
LIMIT n | Keeps at most n values, after sorting. n must be a constant INT64. |
WHERE where_expression | Optional filter inside the call (Preview). |
over_clause | Makes STRING_AGG a window function. DISTINCT, ORDER BY and LIMIT inside the call are then rejected. |
Returns: STRING for STRING input, BYTES for BYTES input. NULL when there are no rows or every value is NULL.
Examples
Products of each order, in line order
WITH order_lines AS (
SELECT 1001 AS order_id, 2 AS line_no, 'mug' AS product UNION ALL
SELECT 1001, 1, 'coffee beans' UNION ALL
SELECT 1001, 3, 'filter papers' UNION ALL
SELECT 1002, 1, 'grinder'
)
SELECT
order_id,
STRING_AGG(product, ', ' ORDER BY line_no) AS products
FROM order_lines
GROUP BY order_id
ORDER BY order_id;| order_id | products |
|---|---|
| 1001 | coffee beans, mug, filter papers |
| 1002 | grinder |
DISTINCT, ORDER BY and LIMIT together
WITH page_views AS (
SELECT 'u1' AS user_id, 'pricing' AS page UNION ALL
SELECT 'u1', 'docs' UNION ALL
SELECT 'u1', 'pricing' UNION ALL
SELECT 'u1', 'blog' UNION ALL
SELECT 'u1', 'careers'
)
SELECT
STRING_AGG(DISTINCT page, ' | ' ORDER BY page) AS all_pages,
STRING_AGG(DISTINCT page, ' | ' ORDER BY page LIMIT 2) AS first_two_pages
FROM page_views;| all_pages | first_two_pages |
|---|---|
| blog | careers | docs | pricing | blog | careers |
NULLs and groups without values
The NULL tag of T-1 is skipped. T-2 has only a NULL tag, so STRING_AGG returns NULL; IFNULL turns that into an empty string.
WITH tickets AS (
SELECT 'T-1' AS ticket_id, 'billing' AS tag UNION ALL
SELECT 'T-1', NULL UNION ALL
SELECT 'T-1', 'refund' UNION ALL
SELECT 'T-2', NULL
)
SELECT
ticket_id,
STRING_AGG(tag, ',' ORDER BY tag) AS tags,
IFNULL(STRING_AGG(tag, ',' ORDER BY tag), '') AS tags_or_empty
FROM tickets
GROUP BY ticket_id
ORDER BY ticket_id;| ticket_id | tags | tags_or_empty |
|---|---|---|
| T-1 | billing,refund | billing,refund |
| T-2 | NULL |
Alert text listing the failed tables of a day
WITH job_failures AS (
SELECT DATE '2026-11-23' AS run_date, 'analytics.orders' AS table_name UNION ALL
SELECT DATE '2026-11-23', 'analytics.customers' UNION ALL
SELECT DATE '2026-11-23', 'analytics.orders'
)
SELECT
run_date,
COUNT(*) AS failures,
CONCAT('Failed: ', STRING_AGG(DISTINCT table_name, ', ' ORDER BY table_name)) AS alert_text
FROM job_failures
GROUP BY run_date;| run_date | failures | alert_text |
|---|---|---|
| 2026-11-23 | 3 | Failed: analytics.customers, analytics.orders |
Common mistakes
- Looking for GROUP_CONCAT or LISTAGG. Neither exists in GoogleSQL; both fail with
Function not found.LISTAGG(x, ',') WITHIN GROUP (ORDER BY y)becomesSTRING_AGG(x, ',' ORDER BY y). - Relying on input order. Without ORDER BY inside the call the order of the pieces is arbitrary and can change between runs; ORDER BY at the end of the query doesn't affect it.
- Combining DISTINCT with ORDER BY on another column.
STRING_AGG(DISTINCT product, ',' ORDER BY line_no)fails withAn aggregate function that has both DISTINCT and ORDER BY arguments can only ORDER BY expressions that are arguments to the function. Sort by the product itself, or deduplicate in a subquery first. - Passing numbers or dates. STRING_AGG accepts only STRING or BYTES and fails with No matching signature for aggregate function STRING_AGG; cast first.
- Expecting a space after the default comma. The default delimiter is
,alone; pass', 'for readable lists.
Cost and performance
- Each group's string is built in memory and has to fit in one row (BigQuery's maximum row size is about 100 MB). A group with millions of values, such as all events of a bot or of a NULL key, can fail with Resources exceeded; add LIMIT inside the call or filter those keys out.
FAQ
What is the BigQuery equivalent of GROUP_CONCAT or LISTAGG?
STRING_AGG: STRING_AGG(name, ', ' ORDER BY name). The ORDER BY goes inside the parentheses; a WITHIN GROUP clause is a syntax error in BigQuery.
How do I sort the values inside STRING_AGG?
Add ORDER BY inside the call, after the delimiter: STRING_AGG(product, ', ' ORDER BY line_no DESC). Several sort keys are allowed.
How do I remove duplicates in STRING_AGG?
Use STRING_AGG(DISTINCT x, ','). If you also sort, the ORDER BY key has to be x itself.
Related
Errors you may hit
More aggregate functions
All BigQuery aggregate functions · All functions · BigQuery error messages
