SQL string aggregation as a window function
String aggregation gains a window context like any aggregate: partition-wide concatenations or frame-local lists beside the detail rows.
- Category
- Window
- Returns
text- In this category
- 68 of 79
Syntax
LISTAGG(name, ', ') WITHIN GROUP (ORDER BY name) OVER (PARTITION BY g) | string_agg(name, ', ') OVER (PARTITION BY g ORDER BY name)
Engine support
| Engine | Support | Notes |
|---|---|---|
| SQL Server | Not built in | STRING_AGG 'is not a valid windowing function'; aggregate in a CTE and join back |
| PostgreSQL | Yes | string_agg OVER works; order via the window's ORDER BY, not inside the call |
| MySQL | Not built in | GROUP_CONCAT is not permitted as a window function; GROUP BY + join, or JSON_ARRAYAGG OVER (8.0.14+) |
| SQLite | Yes | group_concat OVER works (3.25+); no ORDER BY inside the call in window form |
| Oracle | Yes | LISTAGG ... WITHIN GROUP (...) OVER (PARTITION BY ...); no ORDER BY or frame in the OVER |
Examples
Oracle
SELECT department, employee_name,
LISTAGG(employee_name, ', ') WITHIN GROUP (ORDER BY employee_name)
OVER (PARTITION BY department) AS dept_roster
FROM employees;
PostgreSQL
-- ORDER BY goes in the window; the full frame gives every row the whole list
SELECT department, employee_name,
string_agg(employee_name, ', ') OVER (
PARTITION BY department ORDER BY employee_name
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS dept_roster
FROM employees;
Good to know
- List order is set differently per engine: Oracle by WITHIN GROUP (ORDER BY ...), with OVER limited to PARTITION BY. PostgreSQL and SQLite reject ORDER BY inside the call in window form ('aggregate ORDER BY is not implemented for window functions'), so put ORDER BY in the OVER clause and use a full frame.
- SQL Server and MySQL reject string aggregates with OVER outright. GROUP BY the partition key in a CTE to build the list, then join it back to the detail rows.
- Length limits bite in window form too: LISTAGG returns VARCHAR2, so 4000 bytes by default or 32767 with MAX_STRING_SIZE = EXTENDED, and since 12.2 ON OVERFLOW TRUNCATE avoids the ORA-01489 error.
Questions about String Aggregates with OVER
Why does GROUP_CONCAT() OVER fail on MySQL?
MySQL does not permit GROUP_CONCAT as a window function. Aggregate in a CTE and join back, or use JSON_ARRAYAGG(x) OVER (...) if a JSON array will do.
How do I keep the list ordered in a window?
Oracle: LISTAGG(x, sep) WITHIN GROUP (ORDER BY x) OVER (PARTITION BY g). PostgreSQL and SQLite: string_agg / group_concat(x, sep) OVER (PARTITION BY g ORDER BY x ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING). SQL Server: STRING_AGG(x, sep) WITHIN GROUP (ORDER BY x) in a GROUP BY CTE, joined back.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: PostgreSQL, SQLite, 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.