← Back to SQL, page 4

sql-0078 · Medium mid

How do you build a comma-separated list of values per group, in a defined order? Compare GROUP_CONCAT and STRING_AGG.

SQLite has GROUP_CONCAT(expr, separator) and, since 3.44, STRING_AGG as an alias; PostgreSQL has STRING_AGG(expr, separator); MySQL has GROUP_CONCAT. Without an ORDER BY inside the aggregate, the order of items is not guaranteed. PostgreSQL, MySQL and SQLite 3.44+ accept ORDER BY inside the call.

Full answer

List aggregation collapses a group's values into one string, handy for reports and exports.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

SQLite, with an ORDER BY inside the aggregate (supported since 3.44):

sql
SELECT d.name AS dept,
       GROUP_CONCAT(e.name, ', ' ORDER BY e.name) AS people
FROM departments d
JOIN employees e ON e.dept_id = d.id
GROUP BY d.id, d.name
ORDER BY d.name;
deptpeople
EngineeringAsha, Mei, Ravi
SalesSara, Tom
SupportDiego, Omar

PostgreSQL uses string_agg, with the same ORDER BY syntax:

sql
SELECT d.name AS dept,
       string_agg(e.name, ', ' ORDER BY e.name) AS people
FROM departments d
JOIN employees e ON e.dept_id = d.id
GROUP BY d.id, d.name
ORDER BY d.name;
deptpeople
EngineeringAsha, Mei, Ravi
SalesSara, Tom
SupportDiego, Omar

Without the inner ORDER BY, names come out in whatever order the engine reads rows. That order often looks sorted on small data and then changes as the table grows, so tests start failing for no visible reason. An ORDER BY on the outer query does not help; it sorts the result rows, not the items inside each string.

DISTINCT works inside the aggregate too:

sql
SELECT GROUP_CONCAT(DISTINCT dept_id) AS depts_with_staff FROM employees;
depts_with_staff
1,2,3

Two practical limits. MySQL truncates GROUP_CONCAT output at group_concat_max_len (1024 bytes by default, from MySQL documentation, not run here). And a delimited string is for display; if the next step needs the values separately, keep them as rows or use a JSON or array aggregate.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

SELECT d.name AS dept,
       GROUP_CONCAT(e.name, ', ' ORDER BY e.name) AS people
FROM departments d
JOIN employees e ON e.dept_id = d.id
GROUP BY d.id, d.name
ORDER BY d.name;

SELECT GROUP_CONCAT(DISTINCT dept_id) AS depts_with_staff FROM employees;

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • GROUP_CONCAT (SQLite, MySQL) and STRING_AGG (PostgreSQL, SQLite 3.44+) build lists
  • Put ORDER BY inside the aggregate for a defined item order
  • The outer ORDER BY sorts rows, not list items
  • MySQL truncates GROUP_CONCAT at group_concat_max_len

Common mistakes

  • Relying on the natural order of items in the list
  • Splitting the string back into values in later queries

Follow-ups

  • How would you aggregate into a JSON array instead?
  • How do you remove duplicates in the list?

Tips

  • Mention the inner ORDER BY; it is the detail most answers miss