# How to concatenate rows into one string in SQL

Source: https://sqlstreak.com/sql/how-to/concatenate-rows · Tested on PostgreSQL 18.6 and SQLite 3.54.1; results generated 2026-11-22.


**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

```sql
SELECT category,
       string_agg(name, ', ' ORDER BY name) AS items
FROM menu_items
GROUP BY category
ORDER BY category;
```

Result (both engines):

| 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 |

### With a join: what each order contained

```sql
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 (both engines):

| 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 |

### SQLite's older name: group_concat

SQLite also has group_concat. PostgreSQL doesn't.

```sql
SELECT category, group_concat(name, ', ') AS items
FROM menu_items
GROUP BY category
ORDER BY category;
```

PostgreSQL: error: function group_concat(text, unknown) does not exist

SQLite:

| 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 |

## How it works

- `GROUP BY category`: One output row per category.
- `string_agg(name, ', ' …)`: Joins the group's names with a comma and a space.
- `ORDER BY name`: Inside the aggregate: the order of the names in the string.

## 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.
