New versions: Dev (3.16) | Latest (3.15) | 3.14 | 3.13 | 3.12 | 3.11 | 3.10 | 3.9 | 3.8 | Old versions: 3.7 | 3.6 | 3.5 | 3.3

GROUP_CONCAT

Applies to ✅ Open Source Edition   ✅ Express Edition   ✅ Professional Edition   ✅ Enterprise Edition

The GROUP_CONCAT() aggregate function is the MySQL version of the standard SQL LISTAGG function, to concatenate aggregate data into a string. It supports being used with an ORDER BY clause, which uses the expected syntax, unlike LISTAGG(), which uses the WITHIN GROUP syntax.

SELECT
  group_concat(ID),
  group_concat(ID ORDER BY ID),
  group_concat(ID SEPARATOR '; '),
  group_concat(ID ORDER BY ID SEPARATOR '; '),
FROM BOOK
create.select(
         groupConcat(BOOK.ID),
         groupConcat(BOOK.ID).orderBy(BOOK.ID),
         groupConcat(BOOK.ID).separator("; "),
         groupConcat(BOOK.ID).orderBy(BOOK.ID).separator("; "))
      .from(BOOK).fetch();

Producing:

+--------------+--------------+--------------+--------------+
| group_concat | group_concat | group_concat | group_concat |
+--------------+--------------+--------------+--------------+
| 1, 3, 4, 2   | 1, 2, 3, 4   | 1; 3; 4; 2   | 1; 2; 3; 4   |
+--------------+--------------+--------------+--------------+

Dialect support

This example using jOOQ:

groupConcat(BOOK.ID)

Translates to the following dialect specific expressions:

-- AURORA_MYSQL, H2, HSQLDB, MARIADB, MEMSQL, MYSQL
group_concat(BOOK.ID SEPARATOR ',')

-- AURORA_POSTGRES, HANA, POSTGRES
string_agg(CAST(BOOK.ID AS varchar), ',')

-- BIGQUERY, COCKROACHDB
string_agg(CAST(BOOK.ID AS string), ',')

-- DB2, EXASOL, REDSHIFT
listagg(BOOK.ID, ',')

-- ORACLE
listagg(BOOK.ID, ',') WITHIN GROUP (ORDER BY NULL)

-- SQLITE
group_concat(BOOK.ID, ',')

-- SQLSERVER
string_agg(CAST(BOOK.ID AS varchar(max)), ',')

-- SYBASE
list(CAST(BOOK.ID AS varchar), ',')

-- TERADATA
substring(xmlserialize(content xmlagg((',' || CAST(BOOK.ID AS varchar(32000)))) as varchar(32000)) FROM 2)

-- ACCESS, ASE, DERBY, FIREBIRD, IGNITE, INFORMIX, SNOWFLAKE, SQLDATAWAREHOUSE, VERTICA, YUGABYTE
/* UNSUPPORTED */

(These are currently generated with jOOQ 3.16, see #10141), or translate your own on our website

Feedback

Do you have any feedback about this page? We'd love to hear it!

The jOOQ Logo