Skip to the content

The way the clerk walks

Problem

The stock clerk walks the store the same way every time: in through aisle 1, which is Devices; on to aisle 2, Displays, and aisle 3, Audio; and out through aisle 4, Accessories, right by the register. The clerk wants the restocking list to follow that walk, but sorted by section it starts at the register.

Show two columns: the aisle number, and then the product's name. Sort the rows by aisle and, within each aisle, by name. Anything that comes in from a section that doesn't have an aisle yet goes in aisle 5, the one in the back.

What it has to return

These rows, in this order

aislename
1Laptop
1Tablet
2Monitor
3Headphones
3Speaker
4Cable
4Keyboard
4Mouse

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, name
FROM products
ORDER BY category, name;
Solve this challenge

It opens in your browser, with the editor and the tests. It is free and you do not need an account to start.

More O(log n) challenges

See all challenges →