High Price 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
- Asked in
Problem
The analyst renegotiating supplier contracts keeps finding the priciest items buried mid-list in their review of products, when the most expensive should lead. They only negotiate on products priced above 100 and need each one's full record.
Everything above 100.
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
| product_id | product_name | category | price | rating | in_stock |
|---|---|---|---|---|---|
| 10752 | Basic Set v100 | Electronics | 1859.72 | 3.7 | 1 |
| 10703 | Premium Widget v99 | Music | 1850.45 | 2.4 | 0 |
| 10654 | Turbo Set v98 | Garden | 1841.18 | 1.1 | 1 |
| 10605 | Eco Widget v97 | Automotive | 1831.91 | 3.8 | 1 |
| 10262 | Basic Set v90 | Electronics | 1767.02 | 2.7 | 0 |
Worked solution and explanation
Strip the procurement story and this is one predicate with a sort direction attached. Nobody gets the WHERE wrong. Candidates lose this on the edges: they hand-pick four columns because nobody negotiates on in_stock, or they write ORDER BY price and forget it sorts ascending. The grade is the full record, all six columns, most expensive first. Trim a column or flip the sort and 180 correct rows still come back wrong.
The three decisions
Step 1: Filter strictly above 100
price > 100 keeps 180 of the 200 rows in products. It also does a second job quietly: the 9 products with a NULL price fail the comparison, because NULL > 100 is unknown, not true, and the free sample at 0.0 never comes close. No IS NOT NULL guard is needed.
Step 2: Keep every column
The full record means all six columns, product_id through in_stock. The grader compares columns as well as rows, so dropping rating or in_stock because they look irrelevant to pricing fails a query whose rows are perfect.
Step 3: Sort most expensive first
ORDER BY price DESC is the whole ordering contract. Order is graded here, so the same 180 rows sorted ascending is a wrong answer. Above 100 no two products share a price on this data, and the grader only checks that rows are sorted by price, so no tiebreaker is owed.
SELECT *
FROM products
WHERE price > 100
ORDER BY price DESC- Trimmed and backwards
SELECT product_id, product_name, price ... ORDER BY pricereturns the right 180 products, but three columns short and cheapest first: two independent reasons it grades wrong.- Full record, priciest first
SELECT *withORDER BY price DESCreturns every column the team asked for, with 1859.72 on the first row.
Common follow-up questions
- How would you return only the 5 most expensive products? (Adds a `LIMIT`; a tie at the cut would then need a stated rule.)
- How would you restrict this to products currently in stock? (A second predicate on `in_stock`.)
- What if procurement wanted each category's single most expensive product instead? (Moves from a filter to a per-`category` pick.)