Home / SQL / Practice problems / Medium

All products of each category in one line

Medium

Show each category and products: its product names joined with ', ', in alphabetical order of name.

Tables: customers(customer_id, name, city, country, joined) · products(product_id, name, category, price) · orders(order_id, customer_id, order_date, status) · order_items(order_id, product_id, quantity, unit_price)

Your query
Show the expected result
categoryproducts
BooksDeep Learning (book), Python Crash Course (book), SQL Pocket Guide
Electronics27-inch monitor, Mechanical keyboard, Noise-cancelling headphones, USB-C hub, Wireless mouse
FurnitureDesk lamp, Ergonomic chair, Standing desk
GroceryCoffee beans 1 kg, Green tea, 100 bags
StationeryA5 notebook, Gel pens, pack of 10

5 rows

Show a solution
SELECT category, GROUP_CONCAT(name, ', ') AS products FROM (SELECT category, name FROM products ORDER BY name) GROUP BY category;

SQL used

New to SQL? The free SQL course teaches everything these problems use, with a self-checking exercise in every lesson.