SQL function
string_agg() and group_concat(): join values from many rows
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
string_agg(value, separator) is an aggregate that joins the values of a group into one string: string_agg(name, ', ' ORDER BY name). It works in PostgreSQL and in SQLite 3.44 and later; SQLite also has the older group_concat(value, separator).
Syntax and availability
| PostgreSQL | SQLite | |
|---|---|---|
| Available | string_agg: yes. group_concat: no | Both (string_agg since 3.44) |
| Syntax | string_agg(expression, separator [ORDER BY …]) | string_agg(expression, separator [ORDER BY …]) -- or group_concat(expression, separator) |
Examples
Items per category
SELECT category, string_agg(name, ', ' ORDER BY name) AS items
FROM menu_items
GROUP BY category
ORDER BY category;Result (PostgreSQL and SQLite, identical): 4 rows
| category | items |
|---|---|
| dessert | Tiramisu |
| drink | Cola, Lemon Soda |
| pizza | BBQ Meteor, Four Cheese Orbit, Margherita, Pepperoni Rocket, Veggie Nebula |
| side | Caesar Salad, Garlic Knots, Wings |
Try it: PostgreSQL playground · SQLite playground
Customers per city
SELECT city, COUNT(*) AS customers,
string_agg(name, '; ' ORDER BY name) AS names
FROM customers
GROUP BY city
ORDER BY city;Result (PostgreSQL and SQLite, identical): 4 rows
| city | customers | names |
|---|---|---|
| Austin | 3 | Lena Fischer; Maya Ortiz; Priya Nair |
| Boston | 2 | Sam Okafor; Tomás Herrera |
| Chicago | 2 | Marcus Bell; Nora Lindqvist |
| Denver | 3 | Diego Santos; Ellie Park; Jonah Reed |
Try it: PostgreSQL playground · SQLite playground
Unique values only
PostgreSQL accepts DISTINCT inside string_agg. SQLite allows DISTINCT only in one-argument aggregates, so it removes the repeats in a subquery first.
PostgreSQL
SELECT country, string_agg(DISTINCT source, ', ' ORDER BY source) AS sources
FROM users
GROUP BY country
ORDER BY country;SQLite
SELECT country, string_agg(source, ', ' ORDER BY source) AS sources
FROM (SELECT DISTINCT country, source FROM users)
GROUP BY country
ORDER BY country;Result (PostgreSQL and SQLite, identical): 5 rows
| country | sources |
|---|---|
| BR | ad, friend, podcast, search |
| DE | ad, friend, podcast, search |
| GB | ad, friend, podcast, search |
| IN | ad, friend, podcast, search |
| US | ad, friend, podcast, search |
Try it: PostgreSQL playground · SQLite playground
How it behaves: edge cases, measured
Each expression was run as SELECT … on PostgreSQL 18.6 and SQLite 3.54.1 when this page was built. Nothing here is typed by hand.
| Case | PostgreSQL | SQLite |
|---|---|---|
| NULLs are skipped | (SELECT string_agg(x, ',') FROM (SELECT 'a' AS x UNION ALL SELECT NULL UNION ALL SELECT 'b') t)'a,b' | (SELECT string_agg(x, ',') FROM (SELECT 'a' AS x UNION ALL SELECT NULL UNION ALL SELECT 'b') t)'a,b' |
| No rows at all | (SELECT string_agg(x, ',') FROM (SELECT 'a' AS x) t WHERE false)NULL | (SELECT string_agg(x, ',') FROM (SELECT 'a' AS x) t WHERE 0)NULL |
| Ordered inside the aggregate | (SELECT string_agg(x, ',' ORDER BY x DESC) FROM (SELECT 'a' AS x UNION ALL SELECT 'c' UNION ALL SELECT 'b') t)'c,b,a' | (SELECT string_agg(x, ',' ORDER BY x DESC) FROM (SELECT 'a' AS x UNION ALL SELECT 'c' UNION ALL SELECT 'b') t)'c,b,a' |
| Numbers instead of text | (SELECT string_agg(n, ',') FROM (SELECT 1 AS n UNION ALL SELECT 2) t)error: function string_agg(integer, unknown) does not exist | (SELECT string_agg(n, ',') FROM (SELECT 1 AS n UNION ALL SELECT 2) t)'1,2' |
| Numbers cast to text first | (SELECT string_agg(CAST(n AS TEXT), ',') FROM (SELECT 1 AS n UNION ALL SELECT 2) t)'1,2' | (SELECT string_agg(CAST(n AS TEXT), ',') FROM (SELECT 1 AS n UNION ALL SELECT 2) t)'1,2' |
| DISTINCT with a separator | (SELECT string_agg(DISTINCT x, ',') FROM (SELECT 'a' AS x UNION ALL SELECT 'a' UNION ALL SELECT 'b') t)'a,b' | (SELECT string_agg(DISTINCT x, ',') FROM (SELECT 'a' AS x UNION ALL SELECT 'a' UNION ALL SELECT 'b') t)error: DISTINCT aggregates must have exactly one argument |
| group_concat | (SELECT group_concat(x, ',') FROM (SELECT 'a' AS x) t)error: function group_concat(text, unknown) does not exist | (SELECT group_concat(x, ',') FROM (SELECT 'a' AS x UNION ALL SELECT 'b') t)'a,b' |
Related
Common mistakes
No ORDER BY inside the aggregate: the order of the values isn't guaranteed.
Passing numbers to string_agg in PostgreSQL: it wants text. CAST first (see the matrix).
Expecting an empty string for an empty group: you get NULL. Wrap it in
COALESCE(…, '').string_agg(DISTINCT x, ', ')in SQLite: DISTINCT only works with one argument there. Remove repeats in a subquery first.
PostgreSQL and SQLite
string_agg with ORDER BY works in both (SQLite since 3.44). Two differences show up in the matrix: PostgreSQL insists on text arguments while SQLite converts numbers itself, and SQLite refuses DISTINCT when there's a separator argument. group_concat exists only in SQLite.
Official documentation
Go deeper
- Guide: SQL string functions
- Practice: SQL text practice: LIKE patterns and string functions
- Lesson: Text functions (Pro)
Make it stick in five minutes a day
“Text functions” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.