Back to Library

GROUP_CONCAT() in SQL

Concatenates values from multiple rows into a single string, with optional delimiter and ordering.

Syntax

GROUP_CONCAT([DISTINCT] expression [ORDER BY expression] [SEPARATOR delimiter])
Return type
STRING/VARCHAR

GROUP_CONCAT() Function Example

SELECT department, GROUP_CONCAT(employee_name) FROM employees GROUP BY department; 
-- Output: "Sales: John, Mary, Steve"

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

SELECT brand, GROUP_CONCAT(category ORDER BY category) AS categories FROM products GROUP BY brand; 
-- Output:
brand          categories
-----------    ------------------------------------------
Apple          Computers, Phones, Tablets, Watches
Samsung        Home Appliances, Phones, TVs
Sony           Cameras, Gaming, Headphones, Speakers
Dell           Computers, Monitors, Peripherals
LG             Home Appliances, Monitors, TVs

-- Combine skill tags for employees 

SELECT employee_id, GROUP_CONCAT(DISTINCT skill SEPARATOR ', ') AS skills FROM employee_skills GROUP BY employee_id; 


-- Output:
employee_id    name            skills
-----------    -------------   ---------------------------------
E101           John Smith      Java, Python, SQL, TypeScript
E102           Sarah Jones     HTML, JavaScript, React, Vue
E103           Mike Chen       AWS, Docker, Kubernetes, Node.js
E104           Lisa Brown      C++, Java, Python, Ruby
E105           Tom Wilson      MongoDB, MySQL, PostgreSQL, Redis

-- Create comma-separated list of emails 

SELECT department, GROUP_CONCAT(email SEPARATOR '; ') AS email_list FROM employees GROUP BY department;
-- Output:
department    email_list
----------    --------------------------------------------
Engineering   john.s@company.com; mike.c@company.com; 
              tom.w@company.com; lisa.b@company.com

Marketing     sarah.m@company.com; alex.k@company.com; 
              emma.r@company.com

Sales         david.p@company.com; maria.g@company.com; 
              james.l@company.com

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

Related Functions:

STDEV()

Calculates the standard deviation (population or sample) of a set of numeric values.

Calculates the standard deviation (population or sample) of a set of numeric values.

Calculates the standard deviation (population or sample) of a set of numeric values.

Calculates the standard deviation (population or sample) of a set of numeric values.

VARIANCE()

Calculates the statistical variance (population or sample) of a set of numeric values.

Calculates the statistical variance (population or sample) of a set of numeric values.

Calculates the statistical variance (population or sample) of a set of numeric values.

Calculates the statistical variance (population or sample) of a set of numeric values.

STRING_AGG()

Concatenates values from multiple rows into a single string, with specified delimiter.

Concatenates values from multiple rows into a single string, with specified delimiter.

Concatenates values from multiple rows into a single string, with specified delimiter.

Concatenates values from multiple rows into a single string, with specified delimiter.

MAX()

Returns the largest value from a specified column or expression, ignoring NULL values.

Returns the largest value from a specified column or expression, ignoring NULL values.

Returns the largest value from a specified column or expression, ignoring NULL values.

Returns the largest value from a specified column or expression, ignoring NULL values.

MIN()

Returns the smallest value from a specified column or expression, ignoring NULL values.

Returns the smallest value from a specified column or expression, ignoring NULL values.

Returns the smallest value from a specified column or expression, ignoring NULL values.

Returns the smallest value from a specified column or expression, ignoring NULL values.

AVG()

Calculates the arithmetic mean (average) of all non-null values in a specified column or expression.

Calculates the arithmetic mean (average) of all non-null values in a specified column or expression.

Calculates the arithmetic mean (average) of all non-null values in a specified column or expression.

Calculates the arithmetic mean (average) of all non-null values in a specified column or expression.

SUM()

Calculates the total sum of all values in a specified column or expression, ignoring NULL values.

Calculates the total sum of all values in a specified column or expression, ignoring NULL values.

Calculates the total sum of all values in a specified column or expression, ignoring NULL values.

Calculates the total sum of all values in a specified column or expression, ignoring NULL values.

COUNT()

Counts the number of rows or non-null values in a specified column. COUNT(*) counts all rows including nulls, while COUNT(column) counts non-null values in the specified column.

Counts the number of rows or non-null values in a specified column. COUNT(*) counts all rows including nulls, while COUNT(column) counts non-null values in the specified column.

Counts the number of rows or non-null values in a specified column. COUNT(*) counts all rows including nulls, while COUNT(column) counts non-null values in the specified column.

Counts the number of rows or non-null values in a specified column. COUNT(*) counts all rows including nulls, while COUNT(column) counts non-null values in the specified column.

One place for all your queries,
directly on your SQL editor