MySQL
GROUP_CONCAT
SQL functions
database management
SQL limits

MySQL GROUP_CONCAT maximum length and group_concat_max_len

Codemia Knowledge Hub ·

GROUP_CONCAT() results are capped by the group_concat_max_len system variable, which defaults to 1024 bytes. Anything longer is truncated to that length without an error. Raise the limit with SET SESSION group_concat_max_len = ... for the current connection, SET GLOBAL for the server, or in my.cnf to keep it across restarts.

How GROUP_CONCAT() works

GROUP_CONCAT() is an aggregate function like SUM() or COUNT(), but instead of computing a number it concatenates string values from multiple rows into a single string. The typical use is collecting the values of a column for each group into one comma-separated list.

sql
SELECT GROUP_CONCAT(column_name) FROM table_name [WHERE conditions] [GROUP BY column_name];

For a table orders with order_id, customer_id and product_name, this lists each customer's products:

sql
SELECT customer_id, GROUP_CONCAT(product_name SEPARATOR ', ') AS products
FROM orders
GROUP BY customer_id;

Raising group_concat_max_len

sql
SET GLOBAL group_concat_max_len = <new_max_value>;  -- Setting globally
SET SESSION group_concat_max_len = <new_max_value>; -- Setting for current session

<new_max_value> is a positive integer: the byte limit for the concatenated result.

To make the change persistent across server restarts, set it in the MySQL configuration file (my.cnf or my.ini):

 
[mysqld]
group_concat_max_len=<new_max_value>

Then restart the MySQL service.

Configuration VariableDefault ValueMaximum Possible Value
group_concat_max_len1024 bytes18446744073709551615 on 64-bit platforms, 4294967295 on 32-bit

Truncation, character sets and memory

  • Truncation is silent: when the result exceeds group_concat_max_len, MySQL cuts it to the maximum length without failing the query.
  • Character set: the limit is in bytes, so the number of characters you get depends on the character set, since some characters take more than one byte.
  • Performance: a higher limit allows larger strings, which can raise memory consumption when you concatenate large values extensively.
  • Result size: make sure the increase matches what your application needs and does not exceed storage or transmission limits.

For example, with exceptionally long product names:

sql
1SET SESSION group_concat_max_len = 3000;
2
3SELECT customer_id, GROUP_CONCAT(product_name ORDER BY product_name SEPARATOR '; ') AS products
4FROM orders
5GROUP BY customer_id;

Ordering, NULLs and DISTINCT

  • Order: GROUP_CONCAT() respects an ORDER BY inside the call. If order matters, specify it.
  • NULLs: NULL values are skipped. Handling them differently needs extra SQL conditions.
  • DISTINCT: removes duplicate values, for example to list the unique products ordered each day:
sql
1SELECT order_date, 
2       GROUP_CONCAT(DISTINCT product_name ORDER BY product_name ASC SEPARATOR ', ') AS distinct_products
3FROM orders
4GROUP BY order_date;

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design