MySQL String Aggregate Functions: GROUP_CONCAT Explained

Formulas & functions · Updated November 23, 2026

MySQL’s primary string aggregate function is GROUP_CONCAT(), which concatenates values from multiple rows into a single string. For example, SELECT GROUP_CONCAT(name) FROM users; returns all user names in one comma-separated string. This article explains how it works, its syntax, limits, and practical alternatives.

What Are String Aggregate Functions?

Aggregate functions in SQL summarize data across multiple rows into a single result. Standard aggregates like SUM(), AVG(), and COUNT() work on numbers. String aggregates operate on character data, combining text from several rows. MySQL implements this with GROUP_CONCAT(), which is the only built-in string aggregate function. Other databases have similar functions: PostgreSQL uses STRING_AGG(), SQL Server uses STRING_AGG() (since 2017), and Oracle uses LISTAGG(). In MySQL, GROUP_CONCAT() is the go-to tool for turning a column of values into a delimited list.

How GROUP_CONCAT Works

The basic syntax is:

GROUP_CONCAT([DISTINCT] expr [, expr ...] [ORDER BY {unsigned_integer | col_name | expr} [ASC | DESC] [, ...]] [SEPARATOR str_val])

Here’s a simple example. Suppose you have a table student_courses with columns student_id and course:

  • Data: (1, ‘Math’), (1, ‘Science’), (2, ‘Art’)

Running:

SELECT student_id, GROUP_CONCAT(course) AS courses FROM student_courses GROUP BY student_id;

Returns:

  • 1 → ‘Math,Science’
  • 2 → ‘Art’

By default, the separator is a comma. You can change it using SEPARATOR:

SELECT student_id, GROUP_CONCAT(course SEPARATOR ' | ') FROM student_courses GROUP BY student_id;

This returns ‘Math | Science’. You can also sort the concatenated values:

SELECT student_id, GROUP_CONCAT(course ORDER BY course DESC) FROM student_courses GROUP BY student_id;

Result for student 1: ‘Science,Math’. Use DISTINCT to remove duplicates: GROUP_CONCAT(DISTINCT course).

Key Parameters and Limits

Three important settings control GROUP_CONCAT():

  • Separator: Default is comma. Use SEPARATOR to change it. You can use an empty string to concatenate with no delimiter.
  • Maximum length: The result is limited to group_concat_max_len bytes, which defaults to 1024. If the concatenated string exceeds this, MySQL truncates it silently. You can increase it: SET SESSION group_concat_max_len = 100000; or globally.
  • NULL values: GROUP_CONCAT() ignores NULL values. If all values are NULL, the result is NULL.

Example of setting a higher limit:

SET SESSION group_concat_max_len = 10000;

This must be done before the query. For a permanent change, set it in the MySQL configuration file under [mysqld].

Practical Examples and Use Cases

Common scenarios where GROUP_CONCAT() shines:

  • Creating a comma-separated list of IDs for a report.
  • Aggregating tags for a blog post: SELECT post_id, GROUP_CONCAT(tag) FROM post_tags GROUP BY post_id;
  • Building a CSV export directly from SQL.
  • Producing a summary of related items in a single row.

Example with a separator and ordering:

SELECT department, GROUP_CONCAT(employee_name ORDER BY hire_date SEPARATOR '; ') FROM employees GROUP BY department;

This lists employees in each department, sorted by hire date, separated by semicolons.

Common Problems and Misconceptions

One frequent issue is the 1024-byte truncation. Users often see incomplete lists and don’t realize the limit. Always check group_concat_max_len if results seem cut off. Another misconception is that GROUP_CONCAT() can be used without GROUP BY. If you omit GROUP BY, MySQL treats the entire result set as one group, which is valid but often unexpected. For example, SELECT GROUP_CONCAT(name) FROM users; returns all names, which is fine. But if you include other columns, you must use GROUP BY or you’ll get an error (unless ONLY_FULL_GROUP_BY is disabled). Also, GROUP_CONCAT() does not support adding a trailing separator; you have to handle that in application code or with string functions.

Performance and Alternatives

For small datasets, GROUP_CONCAT() is fast. For large datasets, it can be memory-intensive because MySQL builds the entire string in memory. If you need to concatenate millions of rows, consider alternatives:

  • Do the aggregation in application code (e.g., PHP, Python) by fetching rows and joining strings.
  • Use a recursive CTE (available in MySQL 8.0+) if you need more control, though it’s more complex.
  • For very large outputs, write to a file using SELECT ... INTO OUTFILE and process externally.

There is no other built-in string aggregate function in MySQL. If you need to concatenate with a custom delimiter that depends on row values, you may need to use a stored procedure or a user-defined function.

Compatibility and Version Notes

GROUP_CONCAT() has been available since MySQL 4.1. It works in all modern versions, including MySQL 5.7 and 8.0. In MySQL 8.0, the default group_concat_max_len is still 1024, but you can change it. Note that the ORDER BY clause inside GROUP_CONCAT() is a MySQL extension; standard SQL does not define ordering within an aggregate. Also, GROUP_CONCAT() is not available in MariaDB? Actually, MariaDB also supports it, but the exact behavior may vary slightly. Always test on your specific version.

Frequently Asked Questions

How do I concatenate with a newline separator?

Use SEPARATOR '
'
. For example: SELECT GROUP_CONCAT(name SEPARATOR '
') FROM users;
This produces each name on a new line. Note that the newline character is a literal backslash-n in the SQL string, not an actual newline, but MySQL interprets it correctly.

Why is my GROUP_CONCAT result truncated?

Because the default group_concat_max_len is 1024 bytes. Run SHOW VARIABLES LIKE 'group_concat_max_len'; to check. Increase it with SET SESSION group_concat_max_len = 1000000; before running the query.

Can I use GROUP_CONCAT with DISTINCT and ORDER BY together?

Yes. Syntax: GROUP_CONCAT(DISTINCT column ORDER BY column). Example: SELECT GROUP_CONCAT(DISTINCT category ORDER BY category) FROM items; This removes duplicates and sorts the result.

Does GROUP_CONCAT ignore NULL values?

Yes, NULL values are skipped. If all values are NULL, the function returns NULL. To include a placeholder for NULL, use COALESCE(column, 'none') inside.

What is the difference between GROUP_CONCAT and CONCAT?

CONCAT() concatenates values from the same row, e.g., CONCAT(first_name, ' ', last_name). GROUP_CONCAT() concatenates values from multiple rows into one string, typically used with GROUP BY.

Can I use GROUP_CONCAT in a WHERE clause?

No, aggregate functions cannot be used in a WHERE clause. Use HAVING instead. For example, to find groups where the concatenated list contains ‘Math’: SELECT student_id FROM student_courses GROUP BY student_id HAVING GROUP_CONCAT(course) LIKE '%Math%';

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.