GROUP_CONCAT() in SQL
Concatenates values from multiple rows into a single string, with optional delimiter and ordering.
Syntax
Return type
GROUP_CONCAT() Function Example
What is GROUP_CONCAT() in SQL?
The GROUP_CONCAT()
function in SQL concatenates values from multiple rows into a single string, with an optional delimiter and ordering. It is commonly used for aggregating text values, merging related data, and creating comma-separated lists.
This function is available in MySQL, MariaDB, and SQLite, while PostgreSQL uses STRING_AGG()
, and SQL Server uses STRING_AGG()
or FOR XML PATH()
.
The GROUP_CONCAT()
function is widely used in reporting, data aggregation, and text-based analytics.
Parameters:
expression: Column or expression to concatenate
DISTINCT (optional): Concatenate only unique values
ORDER BY (optional): Sort values before concatenation
SEPARATOR (optional): Custom delimiter between values (default is comma)
Example Use Cases:
-- List all product categories per brand
-- Combine skill tags for employees
-- Create comma-separated list of emails
Notes:
Behavior: Skips NULL values in concatenation
Performance Considerations: May have length limitations
Version Info: Syntax varies by DBMS
Deprecated/Recommended Alternatives: STRING_AGG in some DBMS
Error Handling:
Error: Returns NULL if all values are NULL
Recommendation: Use COALESCE for NULL handling
Supported Databases:
DBMS
Function / Syntax
Example
Behavior with NULL
MySQL
GROUP_CONCAT()
GROUP_CONCAT(name)
Ignores NULL
PostgreSQL
STRING_AGG()
STRING_AGG(name, ',')
Ignores NULL
SQL Server
STRING_AGG()
STRING_AGG(name, ',')
Ignores NULL
SQLite
GROUP_CONCAT()
GROUP_CONCAT(name)
Ignores NULL
BigQuery
STRING_AGG()
STRING_AGG(name, ',')
Ignores NULL
Snowflake
LISTAGG()
LISTAGG(name, ',')
Ignores NULL
Athena
ARRAY_JOIN(ARRAY_AGG())
ARRAY_JOIN(ARRAY_AGG(name), ',')
Ignores NULL