sqlstreak

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

categoryitems
dessertTiramisu
drinkCola, Lemon Soda
pizzaBBQ Meteor, Four Cheese Orbit, Margherita, Pepperoni Rocket, Veggie Nebula
sideCaesar Salad, Garlic Knots, Wings

Try it: PostgreSQL playground · SQLite playground

How it works

  1. 1
    GROUP BY category

    One output row per category.

  2. 2
    string_agg(name, ', ' …)

    Joins the group's names with a comma and a space.

  3. 3
    ORDER BY name

    Inside 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_idcontents
12× Lemon Soda, 2× Margherita
21× Pepperoni Rocket, 1× Wings
31× Cola, 1× Four Cheese Orbit, 1× Garlic Knots
42× BBQ Meteor
51× Caesar Salad, 1× Veggie Nebula
62× 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

categoryitems
dessertTiramisu
drinkLemon Soda, Cola
pizzaMargherita, Pepperoni Rocket, Four Cheese Orbit, BBQ Meteor, Veggie Nebula
sideGarlic 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 with CAST(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

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.

Start free