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
| category | products |
|---|---|
| Books | Python Crash Course (book), Deep Learning (book), SQL Pocket Guide |
| Electronics | Wireless mouse, Mechanical keyboard, USB-C hub, 27-inch monitor, Noise-cancelling headphones |
| Furniture | Standing desk, Ergonomic chair, Desk lamp |
| Grocery | Coffee beans 1 kg, Green tea, 100 bags |
| Stationery | A5 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.