MySQL Results as comma separated list
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In MySQL, the standard way to turn multiple row values into one comma-separated string is GROUP_CONCAT(). It is useful for reporting, debugging, and small aggregation tasks, but it needs careful handling around ordering, duplicates, grouping, and maximum output length.
The Basic GROUP_CONCAT Pattern
Suppose you have a table of tags assigned to articles and want one row per article with its tags collapsed into a single string.
If article 10 has rows sql, mysql, and backend, the result becomes a single string such as sql,mysql,backend.
That is the core feature: aggregate many values into one text result.
Control the Separator Explicitly
The default separator is a comma, but you can choose another delimiter:
This is useful when commas already appear inside the data or when the result is meant for human reading instead of machine parsing.
Order the Values Inside the List
Without an explicit order, the concatenated values may appear in an implementation-dependent sequence. If the order matters, specify it inside GROUP_CONCAT:
You can also order by another column, such as creation time or a custom sort position:
This is important for consistent output across runs.
Remove Duplicates With DISTINCT
If joins or source data can produce repeated values, add DISTINCT:
That prevents output like sql,sql,mysql when the real intent was a set of unique values.
Aggregate a Whole Query Into One Row
You do not need GROUP BY if you want one comma-separated list for the entire result set.
That is useful for quick exports, diagnostics, or building small summary strings inside SQL.
Watch group_concat_max_len
A common surprise is truncated output. MySQL limits the maximum length of the generated string through group_concat_max_len.
If the result may be long, increase it for the session:
Then run the query again. If you skip this step, large aggregations may silently cut off text, which can be very misleading.
GROUP_CONCAT Is for Presentation, Not Data Modeling
GROUP_CONCAT is useful when you need a presentation-friendly result or an export-friendly string. It is not a substitute for proper relational design. If downstream code immediately splits the string back into rows, you may be pushing set logic into the wrong layer.
Use it when a string result is the real goal, not as a workaround for missing table structure.
Common Pitfalls
- Forgetting
GROUP BYand getting a single row when you expected grouped output. - Assuming the order inside
GROUP_CONCATis stable without specifyingORDER BY. - Ignoring duplicates that came from joins and should have been removed with
DISTINCT. - Missing truncation because
group_concat_max_lenwas too small. - Using comma-separated strings as a replacement for proper normalized storage.
Summary
- Use
GROUP_CONCAT()to turn multiple MySQL rows into one comma-separated string. - Add
ORDER BYinside the function when output order matters. - Use
DISTINCTwhen duplicate values should be removed. - Change the delimiter with
SEPARATORif commas are not appropriate. - Increase
group_concat_max_lenwhen long results would otherwise be truncated.
Related reading
- MySQL root access from all hosts
- MySQL root password change
- MySQL search and replace some text in a field
- mysql see all open connections to a given database?
- MySQL select 10 random rows from 600K rows fast
- MySQL Select all columns from one table and some from another table
- MySQL Select Date Equal to Today having datetime as the data type
- MySQL Select minimum/maximum among two or more given values

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.