Skip to the content

Aisle by aisle

Problem

The owner is about to walk the store with the restock sheet in hand, and wants to know what's waiting in each aisle: how many products each category has, and how many of them are out of stock, with the stock at 0. Show the category, then how many products it has, then how many are out of stock. Every category goes on the sheet: one with everything in place isn't left off: it gets a 0.

A hint: if every product had a 1 next to it when it's out of stock and a 0 when it isn't, all that would be left is adding them up.

What it has to return

These rows, in any order

categoryhow_manyout_of_stock
Accessories31
Audio21
Devices20
Displays10

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 category,
  COUNT(*) AS how_many
FROM products
GROUP BY 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) challenges

See all challenges →