Create a Comma Separated List from Column Using Select Statement
Estimated study time: 1 minute. Roll up rows into one comma-separated string.
Sometimes you don't want one row per value — you want every value from a column squashed into a single, comma-separated string. SQL Server's STRING_AGG function does exactly this.
Basic Syntax
SELECT STRING_AGG(ColumnName, ', ') AS CombinedList FROM TableName;
Example
SELECT STRING_AGG(EmpName, ', ') AS AllEmployees FROM Employees;
This returns a single row containing every employee name, separated by a comma and a space.
Grouping the List
Combine STRING_AGG with GROUP BY to get one comma-separated list per group instead of one for the whole table:
SELECT DeptID, STRING_AGG(EmpName, ', ') AS EmployeesInDept FROM Employees GROUP BY DeptID;
Ordering Values Inside the List
Use WITHIN GROUP to control the order the values appear in:
SELECT STRING_AGG(EmpName, ', ') WITHIN GROUP (ORDER BY EmpName) FROM Employees;
STRING_AGG is available from SQL Server 2017 onward. On older versions, the same result requires the FOR XML PATH trick instead.The MySQL Way: GROUP_CONCAT
MySQL does not have STRING_AGG; its equivalent is GROUP_CONCAT. The default separator is a comma with no space, and you can change it with the SEPARATOR keyword.
-- Whole table in one string
SELECT GROUP_CONCAT(emp_name ORDER BY emp_name SEPARATOR ', ') AS all_employees
FROM employees;
-- One list per department, without duplicates
SELECT dept_id,
GROUP_CONCAT(DISTINCT city ORDER BY city SEPARATOR ', ') AS cities,
COUNT(*) AS headcount
FROM employees
GROUP BY dept_id;
Notice that ORDER BY and DISTINCT go inside the function call, unlike SQL Server's WITHIN GROUP clause.
Worked Example: Tags Per Blog Post
A many-to-many design stores tags in their own table. Turning them into a display string is a classic use of aggregation:
| post_id | tag |
|---|---|
| 1 | sql |
| 1 | mysql |
| 2 | python |
| 1 | joins |
SELECT p.post_id, p.title,
GROUP_CONCAT(t.tag ORDER BY t.tag SEPARATOR ', ') AS tags
FROM posts p
LEFT JOIN post_tags t ON t.post_id = p.post_id
GROUP BY p.post_id, p.title;
Post 1 returns joins, mysql, sql and post 2 returns python. Thanks to the LEFT JOIN, a post with no tags still appears, with NULL in the tags column; wrap the function in COALESCE(..., '') if you prefer an empty string.
The Same Task in Other Databases
| Database | Function | Ordering |
|---|---|---|
| MySQL / MariaDB | GROUP_CONCAT(col SEPARATOR ', ') | ORDER BY inside the call |
| SQL Server (newer versions) | STRING_AGG(col, ', ') | WITHIN GROUP (ORDER BY ...) |
| PostgreSQL | STRING_AGG(col, ', ') | ORDER BY inside the call |
| Oracle | LISTAGG(col, ', ') | WITHIN GROUP (ORDER BY ...) |
| SQLite | GROUP_CONCAT(col, ', ') | Depends on version |
For older SQL Server versions, the traditional workaround builds an XML string and strips the leading separator with STUFF:
SELECT STUFF((
SELECT ', ' + EmpName
FROM Employees
ORDER BY EmpName
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)'), 1, 2, '') AS AllEmployees;
The TYPE and .value() part stops characters such as & from turning into XML entities in the result.
Gotchas to Keep in Mind
- Silent truncation in MySQL.
GROUP_CONCAToutput is capped by thegroup_concat_max_lensetting, which defaults to 1024 bytes. Long lists are cut off with only a warning. Raise it for the session withSET SESSION group_concat_max_len = 100000;when needed. - NULLs are skipped. All these functions ignore NULL values, so a list may be shorter than the row count.
- Unordered by default. Without an explicit ORDER BY, the order can change between runs.
- Use it for output, not storage. Saving comma-separated values in a column makes searching, joining and updating hard; keep one value per row in tables and build the list only when displaying it.
- Numbers need no casting in MySQL, but in SQL Server concatenating numbers with
+requires converting them to text first.