SQL how-to
How to concatenate rows into one string in SQL
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
string_agg(name, ', ' ORDER BY name) joins the values of each group into one string, in the order you choose. It works in PostgreSQL and in SQLite 3.44+; older SQLite has group_concat(name, ', ').
The task: Combine values from several rows into one string per group, like every item in a category.
Every item in each 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
How it works
- 1
GROUP BY categoryOne output row per category.
- 2
string_agg(name, ', ' …)Joins the group's names with a comma and a space.
- 3
ORDER BY nameInside the aggregate: the order of the names in the string.
More examples
With a join: what each order contained
SELECT oi.order_id,
string_agg(oi.quantity || '× ' || m.name, ', ' ORDER BY m.name) AS contents
FROM order_items oi
JOIN menu_items m ON m.id = oi.item_id
GROUP BY oi.order_id
ORDER BY oi.order_id
LIMIT 6;Result (PostgreSQL and SQLite, identical): 6 rows
| order_id | contents |
|---|---|
| 1 | 2× Lemon Soda, 2× Margherita |
| 2 | 1× Pepperoni Rocket, 1× Wings |
| 3 | 1× Cola, 1× Four Cheese Orbit, 1× Garlic Knots |
| 4 | 2× BBQ Meteor |
| 5 | 1× Caesar Salad, 1× Veggie Nebula |
| 6 | 2× Cola, 2× Garlic Knots, 2× Pepperoni Rocket |
Try it: PostgreSQL playground · SQLite playground
SQLite's older name: group_concat
SQLite also has group_concat. PostgreSQL doesn't.
SELECT category, group_concat(name, ', ') AS items
FROM menu_items
GROUP BY category
ORDER BY category;PostgreSQLERROR: function group_concat(text, unknown) does not exist
SQLite: 4 rows
| category | items |
|---|---|
| dessert | Tiramisu |
| drink | Lemon Soda, Cola |
| pizza | Margherita, Pepperoni Rocket, Four Cheese Orbit, BBQ Meteor, Veggie Nebula |
| side | Garlic Knots, Wings, Caesar Salad |
Try it: PostgreSQL playground · SQLite playground
Variations
Unique values only
PostgreSQL accepts string_agg(DISTINCT city, ', ' ORDER BY city). SQLite allows DISTINCT only in one-argument aggregates, so select the distinct values in a subquery and aggregate those.
Common mistakes
No ORDER BY inside the aggregate: the order of the values isn't guaranteed.
Joining a number with
||in PostgreSQL works only when one side is text; cast withCAST(x AS TEXT)if needed.Very long strings: the result is one value, so huge groups make huge cells.
PostgreSQL and SQLite
string_agg with ORDER BY works in PostgreSQL and SQLite 3.44+, and both produced identical strings. group_concat is SQLite-only (the build shows PostgreSQL's error); PostgreSQL also offers array_agg for an array instead of a string.
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.