Stock Status

An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
easy
Seniority
L3

Problem

The storefront team is redesigning product tiles and wants a clean availability label on each one. For every product, output the product name, category, price, and a label that reads 'In Stock' when the item is available and 'Out of Stock' otherwise. Only include products that have a price on file, ordered alphabetically by product name.

Human-readable availability labels.

Tables

products

Columns: product_id INTEGER, product_name TEXT, category TEXT, price REAL, rating REAL, in_stock INTEGER

products: 10 of 200 rows
product_idproduct_namecategorypriceratingin_stock
1001Basic Device 1XBooks17.522.71
1050Deluxe Bundle 2XClothing25.054.41
1099Pro Unit 3XHome & Kitchen32.582.11
1148Ultra Tool 4XCollectibles40.113.81
1197Essential Set 5XToys47.641.51
1246Classic System 6XBeauty03.21
1295Smart Gadget 7XAutomotive62.74.90
1344Eco Kit 8XGarden70.232.61
1393Turbo Pack 9XMusic77.764.30
1442Premium Widget 10XElectronics85.2921

Expected output

First 5 of 191 rows
product_namecategorypriceavailability
Basic Device 11XBooks92.82In Stock
Basic Device 1XBooks17.52In Stock
Basic Device 21XBooks168.12Out of Stock
Basic Device 31XBooks243.42In Stock
Basic Device 91XBooks695.22Out of Stock

Worked solution and explanation

This is a row-by-row relabel with a completeness gate, dressed up as a storefront redesign. Nothing gets grouped: every product that passes the gate comes out exactly once. Anyone can write the label. What separates candidates is reading 'has a price on file' as an existence test. Nine of the 200 rows in products have a NULL price and must go, while product 1246, a free sample priced at 0.0, has a price and must stay. Reach for price > 0 and you quietly ship 190 tiles instead of 191.

Walking to the answer

Step 1: Gate on whether price exists

WHERE price IS NOT NULL asks the only question the storefront cares about: is there a number to print? A value test like price > 0 answers a different question, and on this seed it costs you the free sample. The 0.0 row sits in the sample data, so check it before you decide.

Step 2: Turn the 1/0 flag into the tile text

in_stock is an INTEGER, not a status string, so the label keys off the number: CASE WHEN in_stock = 1 THEN 'In Stock' ELSE 'Out of Stock' END. Alias it availability, the header the expected output uses. Selecting the raw column gives the right rows with every label wrong.

Step 3: Sort by product_name as text

ORDER BY product_name compares character by character, which is why 'Basic Device 11X' comes before 'Basic Device 1X' in the preview. That is the expected order, not a bug to work around; pulling the trailing number out of the name would answer a question nobody asked.

Filter, relabel, sort
SELECT product_name, category, price, CASE WHEN in_stock = 1 THEN 'In Stock' ELSE 'Out of Stock' END AS availability
FROM products
WHERE price IS NOT NULL
ORDER BY product_name
Value test
WHERE price > 0 drops the 9 NULL prices and also the free sample at 0.0, returning 190 rows. WHERE price != NULL is worse: the comparison is unknown on every row, so nothing survives.
Existence test
WHERE price IS NOT NULL drops exactly the 9 missing prices and keeps the 0.0 row, returning all 191 tiles the wall needs.

Common follow-up questions