# string_agg() and group_concat(): join values from many rows

Source: https://sqlstreak.com/sql/functions/string-agg · Tested on PostgreSQL 18.6 and SQLite 3.54.1; results generated 2026-11-22.


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

- PostgreSQL: `string_agg(expression, separator [ORDER BY …])` (string_agg: yes. group_concat: no)
- SQLite: `string_agg(expression, separator [ORDER BY …])  -- or group_concat(expression, separator)` (Both (string_agg since 3.44))

## Examples

### Items per 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 |

### Customers per city

```sql
SELECT city, COUNT(*) AS customers,
       string_agg(name, '; ' ORDER BY name) AS names
FROM customers
GROUP BY city
ORDER BY city;
```

Result (both engines):

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

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

```sql
SELECT country, string_agg(DISTINCT source, ', ' ORDER BY source) AS sources
FROM users
GROUP BY country
ORDER BY country;
```

SQLite:

```sql
SELECT country, string_agg(source, ', ' ORDER BY source) AS sources
FROM (SELECT DISTINCT country, source FROM users)
GROUP BY country
ORDER BY country;
```

Result (both engines):

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

## Edge cases, measured on both engines

| Case | PostgreSQL | SQLite |
| --- | --- | --- |
| NULLs are skipped | 'a,b' | 'a,b' |
| No rows at all | NULL | NULL |
| Ordered inside the aggregate | 'c,b,a' | 'c,b,a' |
| Numbers instead of text | error: function string_agg(integer, unknown) does not exist | '1,2' |
| Numbers cast to text first | '1,2' | '1,2' |
| DISTINCT with a separator | 'a,b' | error: DISTINCT aggregates must have exactly one argument |
| group_concat | error: function group_concat(text, unknown) does not exist | 'a,b' |

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