Inventory Saturday
- O(n log n) · Hard
- Full plan
- SQL
- subqueries
- grouping
- joins
Problem
Saturday is inventory day, and one morning isn't enough to count everything. The owner starts with what moves: the products somebody has ordered at least once. The question is how many units are waiting in each section.
Show the category and then how many units are in stock across its products that have been ordered. In the warehouse each product is there once, however many orders it has. The order dated the 20th has a mistyped date, but the order exists: it counts. A section nothing has been ordered from isn't on the list.
What it has to return
These rows, in any order
| category | units |
|---|---|
| Accessories | 42 |
| Audio | 15 |
| Devices | 10 |
| Displays | 5 |
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, p.name,
p.stock
FROM orders o
JOIN products p
ON o.product_id = p.id;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.