Home / SQL / Reference / Aggregating

SQL GROUP_CONCAT

Join the values of a group into one text value.

Syntax

GROUP_CONCAT(expression, separator)

Example

Query (on the shop database used in the SQL course)

SELECT category, GROUP_CONCAT(name, ', ') AS products
FROM products
GROUP BY category;

Result

categoryproducts
BooksPython Crash Course (book), Deep Learning (book), SQL Pocket Guide
ElectronicsWireless mouse, Mechanical keyboard, USB-C hub, 27-inch monitor, Noise-cancelling headphones
FurnitureStanding desk, Ergonomic chair, Desk lamp
GroceryCoffee beans 1 kg, Green tea, 100 bags
StationeryA5 notebook, Gel pens, pack of 10

5 rows

Good to know. PostgreSQL: STRING_AGG(name, ', '). MySQL: GROUP_CONCAT(name SEPARATOR ', ').

Related

Learn it step by step in the free lesson Summarising Data, with exercises that check your queries.