Menu

SQLite group_concat() Function

The SQLite group_concat() aggregate function combines non-NULL values from multiple rows into one text value. It is useful for listing a customer’s purchases, a user’s hobbies, or the tags associated with each record.

Syntax

group_concat(value)
group_concat(value, separator)
group_concat(value ORDER BY sort_expression)
group_concat(value, separator ORDER BY sort_expression)
group_concat(DISTINCT value)
  • value is the expression to concatenate. NULL values are omitted.
  • separator is optional. SQLite uses a comma (,) by default.
  • ORDER BY inside the function determines the order of concatenated values. This syntax is available in SQLite 3.44.0 and later.
  • DISTINCT removes duplicate non-NULL values. When using DISTINCT, group_concat() accepts only one argument, so the default separator applies.

The result is TEXT, or SQL NULL if there are no non-NULL values to concatenate. Without an ORDER BY inside the aggregate, the input order is not guaranteed.

Examples

Combine values for each group

Suppose a table contains a row for each person’s hobby. This self-contained example creates sample rows with a common table expression (CTE):

WITH hobbies(name, hobby, sort_order) AS (
    VALUES
        ('Tim', 'Football', 2),
        ('Tim', 'Baseball', 1),
        ('Tom', 'Piano', 1),
        ('Tom', NULL, 2),
        ('Tom', 'Violin', 3)
)
SELECT
    name,
    group_concat(hobby, ' | ' ORDER BY sort_order) AS hobbies
FROM hobbies
GROUP BY name
ORDER BY name;
name  hobbies
----  -------------------
Tim   Baseball | Football
Tom   Piano | Violin

GROUP BY produces one result per person, ' | ' supplies a readable separator, and ORDER BY sort_order makes the value order predictable. Tom’s NULL hobby is excluded.

Use the default comma separator

WITH tags(tag) AS (
    VALUES ('sqlite'), ('json'), ('sql')
)
SELECT group_concat(tag ORDER BY tag) AS tags
FROM tags;
tags
---------------
json,sql,sqlite

Ordering the final SELECT rows is not a substitute for ordering the values inside group_concat(). Put ORDER BY in the aggregate whenever the text order matters.

Remove duplicate values

WITH tags(tag) AS (
    VALUES ('sql'), ('sql'), ('sqlite')
)
SELECT group_concat(DISTINCT tag ORDER BY tag) AS tags
FROM tags;
tags
----------
sql,sqlite

group_concat(DISTINCT tag, ' | ') is not valid SQLite syntax: a DISTINCT aggregate must have exactly one argument. If you need both unique values and a custom separator, deduplicate in a subquery or CTE first and then call group_concat(tag, ' | ' ORDER BY tag).

What happens with NULL or no rows?

WITH values_to_join(value) AS (
    VALUES (NULL), ('A'), (NULL), ('B')
)
SELECT group_concat(value, '-' ORDER BY value) AS combined
FROM values_to_join;
combined
--------
A-B

Only non-NULL values participate. When every value is NULL, or when the group has no rows, group_concat() returns SQL NULL, not an empty string.

Frequently asked questions

Why does group_concat() produce a different order?

Without an aggregate ORDER BY, SQLite does not promise any particular order, even when a query appears to return consistent results. Use group_concat(value ORDER BY sort_expression) on SQLite 3.44.0 or later.

Is group_concat() the same as string_agg()?

SQLite also supports string_agg(value, separator) as an alias for the two-argument form of group_concat(). Both accept an aggregate ORDER BY in supported versions.

Try it in your browser

Run or edit these SQL examples in the SQLite Playground, which currently uses SQLite 3.49.1. For the exact aggregate syntax, see the SQLite documentation and its 3.44.0 release notes.