Full answer
List aggregation collapses a group's values into one string, handy for reports and exports.
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):
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;| dept | people |
|---|---|
| Engineering | Asha, Mei, Ravi |
| Sales | Sara, Tom |
| Support | Diego, Omar |
PostgreSQL uses string_agg, with the same ORDER BY syntax:
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;| dept | people |
|---|---|
| Engineering | Asha, Mei, Ravi |
| Sales | Sara, Tom |
| Support | Diego, 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:
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.
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