Budget-Friendly Products

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 mid-price promotion will feature every product category that stocks at least one item priced between 90 and 200, and the merchandiser building it wants those categories listed alphabetically.

Affordable does not mean invisible.

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 10 rows
category
Automotive
Beauty
Books
Clothing
Collectibles

Worked solution and explanation

What this really tests

This is an inclusive range filter feeding a duplicate collapse, dressed up as promotion planning. The question underneath: can you turn 'which aisles stock something in this price band' into a repeat-free category list? Ten categories qualify, but four of them (Books, Clothing, Home & Kitchen, Toys) carry two in-band products each, so a plain SELECT category hands the merchandiser 14 rows with those four listed twice. DISTINCT is what collapses 14 rows to the 10 aisles that qualify. The quieter finding: Music has no priced item in the band, so it drops out entirely rather than showing up empty.


Working through it

Step 1: Filter to the mid-price band

Keep only rows whose price sits in the 90 to 200 band. BETWEEN reads cleanly and includes both endpoints. The filter does two jobs at once: it thins each category to its mid-priced items, and it removes Music, whose one product in that stretch of the catalog has no recorded price and so never passes the comparison.

Step 2: Project category and collapse repeats

Project category, not the product. Four categories survive the filter with two products each, so this projection has repeats by construction and DISTINCT does real work: it turns 'every qualifying product's category' into 'the set of aisles that qualify'.

Step 3: Sort alphabetically

Order by category so the list reads A to Z, the order the preview shows and the grader checks.


The solution

Distinct categories with a mid-price item
SELECT DISTINCT category
FROM products
WHERE price BETWEEN 90 AND 200
ORDER BY category

Common follow-up questions