SQL STRING_AGG / GROUP_CONCAT function
Concatenates the values of a group into one delimited string. All five engines have it, under three names, with different ordering syntax and different length limits.
- Category
- Aggregates
- Returns
string- In this category
- 7 of 52
Syntax
STRING_AGG(expr, sep) WITHIN GROUP (ORDER BY ...)
GROUP_CONCAT(expr SEPARATOR sep)
LISTAGG(expr, sep) WITHIN GROUP (ORDER BY ...)
Each block is a separate form. The support table and the examples show which engine takes which.
Engine support
| Engine | Support | Notes |
|---|---|---|
| SQL Server | Yes | STRING_AGG from SQL Server 2017 |
| PostgreSQL | Yes | Supported under this name. |
| MySQL | Different name or syntax | GROUP_CONCAT, with group_concat_max_len capping the result |
| SQLite | Yes | GROUP_CONCAT, plus STRING_AGG as an alias and ORDER BY inside the call from 3.44 |
| Oracle | Different name or syntax | LISTAGG; ON OVERFLOW TRUNCATE from 12.2, DISTINCT from 19c |
Examples
SQL Server
SELECT customer_id,
STRING_AGG(product, ', ') WITHIN GROUP (ORDER BY product) AS basket
FROM order_items
GROUP BY customer_id;
PostgreSQL
SELECT customer_id,
STRING_AGG(product, ', ' ORDER BY product) AS basket
FROM order_items
GROUP BY customer_id;
MySQL
SELECT customer_id,
GROUP_CONCAT(product ORDER BY product SEPARATOR ', ') AS basket
FROM order_items
GROUP BY customer_id;
SQLite
-- ORDER BY inside the call needs SQLite 3.44+
SELECT customer_id,
GROUP_CONCAT(product, ', ' ORDER BY product) AS basket
FROM order_items
GROUP BY customer_id;
Oracle
SELECT customer_id,
LISTAGG(product, ', ') WITHIN GROUP (ORDER BY product) AS basket
FROM order_items
GROUP BY customer_id;
Good to know
- Ordering syntax differs: WITHIN GROUP (ORDER BY ...) on SQL Server and Oracle, ORDER BY inside the call on PostgreSQL, MySQL and SQLite 3.44+. Older SQLite has no ordering option.
- MySQL truncates at group_concat_max_len, 1024 bytes by default, with only a warning. Raise it per session: SET SESSION group_concat_max_len = 1000000.
- SQL Server's STRING_AGG raises an error past 8000 bytes for varchar input; CAST the value to varchar(max) to lift the limit. Oracle's LISTAGG raises ORA-01489 when the result outgrows VARCHAR2 (4000 bytes by default) unless you add ON OVERFLOW TRUNCATE.
- DISTINCT inside the call works on PostgreSQL, MySQL, SQLite and Oracle 19c+. SQL Server's STRING_AGG has no DISTINCT; dedupe in a subquery first.
Questions about STRING_AGG / GROUP_CONCAT
Which engines use STRING_AGG and which use GROUP_CONCAT?
SQL Server (2017+) and PostgreSQL call it STRING_AGG. MySQL calls it GROUP_CONCAT. SQLite has GROUP_CONCAT and, from 3.44, STRING_AGG as well. Oracle calls it LISTAGG.
Why is my GROUP_CONCAT result truncated?
MySQL cuts the string at group_concat_max_len, 1024 bytes by default, and only raises a warning. Raise the variable in your session. SQL Server and Oracle raise an error at their length limits instead of truncating.
How do I keep the values in order inside the string?
SQL Server and Oracle need WITHIN GROUP (ORDER BY ...). PostgreSQL, MySQL and SQLite 3.44+ take ORDER BY inside the call. Before 3.44, SQLite has no ordering option, and sorting in a subquery first is common but not guaranteed to hold.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
- Join child rows into a comma-separated list in SQL Snippet
- List enum types and their values Snippet
- Table dependency order for load and dump Snippet
- Generate DROP TABLE statements Snippet
- Truncate all tables in safe order Snippet
- SQL aggregation & HAVING interview questions Interview questions
Cheat sheets for the engines that have it: PostgreSQL, MySQL, SQLite, SQL Server, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.