Skip to the content

What sells and what sits

Problem

Having a product in the catalog isn't the same as selling it. The owner wants to see, section by section, how much of what the store carries has actually been ordered up to today, January 15. The order dated the 20th doesn't count yet.

Show three columns, in this order: the category, how many products it has, and how many of them have been ordered at least once. A product ordered five times is one product ordered, and the category shows up even if none of its products has moved.

What it has to return

These rows, in any order

categorytotalordered
Accessories33
Audio21
Devices21
Displays11

This is what comes out with the data in the practice database this challenge uses. The column names don’t count, so name yours however you like: what counts is how many there are and the order they come in. Besides this one, the challenge has hidden tests with other data that are revealed when you submit your solution.

You start with this

SQL

SELECT p.category,
  COUNT(*) AS total
FROM products p
GROUP BY p.category;
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n log n) challenges

See all challenges →