groupconcatenation
Group concatenation is a data aggregation operation that combines multiple string values from rows into a single string for each group defined by a grouping key. It is commonly used to collapse many rows into a list per group, such as listing all employees in a department or all tags associated with a post. The exact function name and syntax vary by database system, but the core idea is the same: aggregate strings with a delimiter to produce one concatenated result per group.
Common implementations by database system include: MySQL uses GROUP_CONCAT, PostgreSQL uses STRING_AGG, Oracle uses LISTAGG, and
- MySQL: SELECT department_id, GROUP_CONCAT(employee_name ORDER BY employee_name SEPARATOR ', ') AS employees FROM employees GROUP BY department_id;
- PostgreSQL: SELECT department_id, STRING_AGG(employee_name, ', ' ORDER BY employee_name) AS employees FROM employees GROUP BY department_id;
Limitations and considerations include maximum result length in some systems (for example, MySQL has a configurable
Uses of group concatenation include creating denormalized lists for reports, generating comma-separated item lists, and enabling