All problems

How Deep Is Each Product Category?

easySQLGroupBy

Whetcode Supply's merchandising team is deciding which part of the catalogue to expand next, and the first thing they want on the table is how thin or deep each category already is.

The categories are not held in a list of their own. Each product row carries its own category as a short text label, so the set of categories has to come out of the product rows themselves — which also means an empty category is not something this data can show you.

products — one row per product on sale. category is the shelf it sits on. price is in dollars.

id name category price
1 Wireless Mouse electronics 25.0
2 Standing Desk furniture 350.0
3 Desk Lamp furniture 40.0
4 Mechanical Keyboard electronics 85.0
5 Notebook Set office 12.0

The table already exists in the database — there is nothing to create or load.

Task: Write a query returning two columns, category and num_products, with one row for every category that has at least one product in it. A category with nothing in it produces no row rather than a row reading zero. Rows come back in alphabetical order of category.

Example output — shape only; the figures below are invented, not this data's answer.

category num_products
lighting 3
stationery 6
storage 9

Sign in to solve this problem

Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.

Sign in

Discussion

Sign in to join the discussion — reading is open to everyone.

Loading comments…