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
| product_id | product_name | category | price | rating | in_stock |
|---|---|---|---|---|---|
| 1001 | Basic Device 1X | Books | 17.52 | 2.7 | 1 |
| 1050 | Deluxe Bundle 2X | Clothing | 25.05 | 4.4 | 1 |
| 1099 | Pro Unit 3X | Home & Kitchen | 32.58 | 2.1 | 1 |
| 1148 | Ultra Tool 4X | Collectibles | 40.11 | 3.8 | 1 |
| 1197 | Essential Set 5X | Toys | 47.64 | 1.5 | 1 |
| 1246 | Classic System 6X | Beauty | 0 | 3.2 | 1 |
| 1295 | Smart Gadget 7X | Automotive | 62.7 | 4.9 | 0 |
| 1344 | Eco Kit 8X | Garden | 70.23 | 2.6 | 1 |
| 1393 | Turbo Pack 9X | Music | 77.76 | 4.3 | 0 |
| 1442 | Premium Widget 10X | Electronics | 85.29 | 2 | 1 |
Expected output
| 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
SELECT DISTINCT category
FROM products
WHERE price BETWEEN 90 AND 200
ORDER BY categoryCommon follow-up questions
- The campaign now wants how many qualifying products sit in each category, not just the category names. How does the query change? (Tests moving from a set projection to a genuine aggregation.)
- Marketing also wants a list of the categories that stock nothing in the 90 to 200 band, like Music. How would you surface those? (Tests reasoning about categories absent from the result.)
- `category` is heavily skewed, with a few values dominating. Does `DISTINCT` or `GROUP BY` win on cost here, and does the difference matter at this table size? (Tests cost intuition about deduplicating a skewed low-cardinality column.)