SQL LISTAGG function
Aggregates column values into one delimited string per group, ordered how you choose. Oracle's answer to STRING_AGG and GROUP_CONCAT.
- Category
- Aggregates
- Returns
string- In this category
- 11 of 52
Syntax
LISTAGG(column, separator) WITHIN GROUP (ORDER BY ...)
Engine support
| Engine | Support | Notes |
|---|---|---|
| SQL Server | Different name or syntax | STRING_AGG (2017+) |
| PostgreSQL | Different name or syntax | STRING_AGG |
| MySQL | Different name or syntax | GROUP_CONCAT |
| SQLite | Different name or syntax | group_concat() |
| Oracle | Yes | Supported under this name. |
Examples
Oracle
SELECT department, LISTAGG(last_name, ', ') WITHIN GROUP (ORDER BY last_name) AS team FROM employees GROUP BY department;
Oracle
SELECT LISTAGG(tag, ', ' ON OVERFLOW TRUNCATE '...' WITH COUNT)
WITHIN GROUP (ORDER BY tag) AS tags
FROM bookmarks;
Good to know
- ORA-01489: the result cannot exceed 4000 bytes (32767 with MAX_STRING_SIZE=EXTENDED). Since 12.2, LISTAGG(col, ', ' ON OVERFLOW TRUNCATE '...' WITH COUNT) cuts the list off instead of failing.
- Order is unspecified without WITHIN GROUP (ORDER BY ...). Always spell it out for stable output.
- NULL values are skipped, and a group with only NULLs aggregates to NULL, not ''.
- WMSYS.WM_CONCAT is the old undocumented workaround, unsupported and gone in modern releases; use LISTAGG.
Questions about LISTAGG
Why do I get ORA-01489 with LISTAGG?
The concatenated result passed 4000 bytes. Switch to MAX_STRING_SIZE=EXTENDED, aggregate fewer or shorter values, or add ON OVERFLOW TRUNCATE inside the LISTAGG parentheses (12.2+).
How do I remove duplicates in LISTAGG?
LISTAGG(DISTINCT column, sep) works from Oracle 19c. Before that, deduplicate in a subquery first.
What is the PostgreSQL equivalent?
STRING_AGG(column, separator ORDER BY ...). MySQL's GROUP_CONCAT and SQLite's group_concat cover the same ground with different clauses.
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
- SQL aggregation & HAVING interview questions Interview questions
Cheat sheets for the engines that have it: Oracle, SQL Server, PostgreSQL, MySQL, SQLite.
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.