GROUP_CONCAT ORDER BY
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
GROUP_CONCAT in MySQL concatenates values from multiple rows into a single string, grouped by a specified column. Adding ORDER BY inside GROUP_CONCAT controls the order of the concatenated values. This is different from an ORDER BY on the outer query — the outer ORDER BY sorts the result rows, while ORDER BY inside GROUP_CONCAT sorts the values within each concatenated string. This function is MySQL-specific; PostgreSQL uses STRING_AGG() and SQL Server uses STRING_AGG() or FOR XML PATH.
Basic Syntax
Example: Students and Their Courses
Result:
| student_name | courses |
| Alice | Chemistry, Math, Physics |
| Bob | History, Math, Physics |
| Charlie | Chemistry, Math |
Without ORDER BY, the concatenation order is undefined.
ORDER BY with Different Columns
| student_name | courses_by_grade |
| Alice | Chemistry, Math, Physics |
| Bob | History, Physics, Math |
| Charlie | Math, Chemistry |
Alice's A-grade courses (Chemistry, Math) come before B-grade (Physics).
Multiple ORDER BY Columns
DISTINCT with ORDER BY
DISTINCT removes duplicate values before concatenation. The ORDER BY applies after deduplication.
Custom Separator
Concatenating Multiple Columns
| student_name | course_grades |
| Alice | Chemistry (A), Math (A), Physics (B) |
| Bob | History (A), Math (C), Physics (B) |
Increasing the Length Limit
GROUP_CONCAT has a default maximum length of 1024 characters. Longer results are silently truncated.
PostgreSQL Equivalent: STRING_AGG
Note: In STRING_AGG, the separator comes before ORDER BY, unlike MySQL's GROUP_CONCAT where ORDER BY comes before SEPARATOR.
SQL Server Equivalent
Common Pitfalls
- Silent truncation at 1024 characters: The default
group_concat_max_lenis 1024. Results longer than this are silently truncated with no warning. Always increase this setting when concatenating many values or long strings. - Confusing inner ORDER BY with outer ORDER BY:
GROUP_CONCAT(col ORDER BY col)orders values within the concatenated string.ORDER BYat the end of the query orders the result rows. They are independent and serve different purposes. - NULL values:
GROUP_CONCATignores NULL values. If a column contains NULLs, they are silently excluded from the concatenated result. UseCOALESCE(column, 'N/A')if you want to include a placeholder for NULLs. - Not using DISTINCT when needed: If joins produce duplicate rows,
GROUP_CONCATincludes duplicates in the output. UseGROUP_CONCAT(DISTINCT ...)to eliminate them. - Assuming GROUP_CONCAT exists in all databases:
GROUP_CONCATis MySQL/MariaDB-specific. PostgreSQL usesSTRING_AGG(), SQL Server 2017+ usesSTRING_AGG()withWITHIN GROUP, and SQLite usesGROUP_CONCAT()(with different syntax for ordering).
Summary
GROUP_CONCAT(col ORDER BY sort_col SEPARATOR ', ')concatenates and sorts values within each group- The inner
ORDER BYsorts the concatenated values; the outerORDER BYsorts result rows - Use
DISTINCTto remove duplicates before concatenation - Default max length is 1024 characters — increase
group_concat_max_lenfor longer results - PostgreSQL equivalent:
STRING_AGG(col, ', ' ORDER BY col) - SQL Server equivalent:
STRING_AGG(col, ', ') WITHIN GROUP (ORDER BY col)
Related reading
- Group result by 15 minutes time interval in MongoDb
- H2-Console is not showing in browser
- H2-In memory database console not opening
- H2 Console throwing a error webAllowOthers in H2 database
- H2 database console spring boot Load denied by X-Frame-Options
- H2 Database not found error 90146. H2 database is not created on start
- H2 in-memory database. Table not found
- Hadoop on cassandra database

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.