MySQL
SQL Query
Database Management
Data Formatting
Comma Separated Values

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.

Practice system design

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.

sql
1SELECT
2  article_id,
3  GROUP_CONCAT(tag_name) AS tags
4FROM article_tags
5GROUP BY article_id;

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:

sql
1SELECT
2  article_id,
3  GROUP_CONCAT(tag_name SEPARATOR ' | ') AS tags
4FROM article_tags
5GROUP BY article_id;

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:

sql
1SELECT
2  article_id,
3  GROUP_CONCAT(tag_name ORDER BY tag_name ASC) AS tags
4FROM article_tags
5GROUP BY article_id;

You can also order by another column, such as creation time or a custom sort position:

sql
1SELECT
2  article_id,
3  GROUP_CONCAT(tag_name ORDER BY sort_order ASC) AS tags
4FROM article_tags
5GROUP BY article_id;

This is important for consistent output across runs.

Remove Duplicates With DISTINCT

If joins or source data can produce repeated values, add DISTINCT:

sql
1SELECT
2  article_id,
3  GROUP_CONCAT(DISTINCT tag_name ORDER BY tag_name) AS tags
4FROM article_tags
5GROUP BY article_id;

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.

sql
SELECT GROUP_CONCAT(name ORDER BY name) AS employee_names
FROM employees;

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:

sql
SET SESSION group_concat_max_len = 100000;

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 BY and getting a single row when you expected grouped output.
  • Assuming the order inside GROUP_CONCAT is stable without specifying ORDER BY.
  • Ignoring duplicates that came from joins and should have been removed with DISTINCT.
  • Missing truncation because group_concat_max_len was 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 BY inside the function when output order matters.
  • Use DISTINCT when duplicate values should be removed.
  • Change the delimiter with SEPARATOR if commas are not appropriate.
  • Increase group_concat_max_len when long results would otherwise be truncated.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.