SQLite 3 reference · Aggregate Functions
SQLite STRING_AGG() Function
Join a group's values with a separator, the standard name also used by PostgreSQL and SQL Server.
SQLite 3 Since 3.44.0
Syntax
string_agg(X, SEP ORDER BY ...)
Parameters
| Parameter | Description |
|---|---|
X |
A column or expression. |
SEP |
The separator (required). |
ORDER BY optional |
The order of the values. |
Return value
Text, or NULL for no values.
Examples
SELECT department, string_agg(name, ', ' ORDER BY salary DESC) AS by_salary
FROM employees
GROUP BY department;
Output
department | by_salary Engineering | Omar Haddad, Mei Chen, Jonas Berg, Priya Nair, Lucas Martin Management | Sofia Reyes Sales | Hannah Okafor, Diego Silva, Emma Novak Support | Ravi Kumar
SELECT string_agg(country, '/') AS countries
FROM (SELECT DISTINCT country FROM customers ORDER BY country);
Output
countries Japan/Netherlands/Switzerland/UK/USA
Version history
| Version | Change |
|---|---|
| SQLite 3.44.0 | Added |
In other languages
STRING_AGG()· PostgreSQL
Related
GROUP_CONCAT()· Join a group's non-NULL values into one string, separated by commas or a separator you give.JSON_GROUP_ARRAY()· Aggregate the values of a group into one JSON array, one element per row. SQL NULLs become JSON null.- SQL Aggregate Functions · tutorial
- SQL GROUP BY · tutorial