Price Check
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
Every product in products should carry a price. Which categories hold products that break that rule, how many does each hold, and what is the average rating of those unpriced products, so the pricing team knows where to backfill?
Priced to sell or priced to sit?
Tables
products
Columns: product_id INTEGER, product_name TEXT, category TEXT, price REAL, rating REAL, in_stock BOOLEAN
| 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 | unpriced_products | avg_rating |
|---|---|---|
| Automotive | 1 | 1.9 |
| Beauty | 2 | 2.35 |
| Clothing | 1 | 2.3 |
| Garden | 1 | 1.6 |
| Music | 2 | 2.85 |
Worked solution and explanation
What this really is
This is a completeness check dressed up as a pricing question. The rule is that every row of products carries a price; the report is the rows that break it, summarized per category. Anyone can write the GROUP BY. The trap is that the thing you are reporting is a NULL, and NULL defeats the 2 moves that feel natural: WHERE price = NULL is never true and hands back an empty report, and COUNT(price) over the failing rows counts zero in every category, because counting a column skips exactly the values you came to count.
Walking to the answer
Step 1: Isolate the rows that fail the rule
9 products have a NULL price, spread over Music, Beauty, Toys, Garden, Automotive and Clothing. WHERE price IS NULL keeps exactly those. A comparison with = is never true against NULL, so the only way to find a missing value is to ask whether it is missing.
Step 2: Leave the zero price alone
Beauty holds a free sample priced at zero. It has a price on file, so it passes the check. Adding OR price = 0 would put Beauty at 3 failures and fold a priced product into its average rating.
Step 3: Count rows, average the rating
COUNT(*) becomes unpriced_products: Music, Beauty and Toys 2 each, Garden, Automotive and Clothing 1. AVG(rating) becomes avg_rating, the rating of those same products, which tells the team how much shoppers like what cannot be sold yet: Toys averages 3.35 and Garden 1.6.
SELECT
category,
COUNT(*) AS unpriced_products,
AVG(rating) AS avg_rating
FROM products
WHERE price IS NULL
GROUP BY category- Count the column
COUNT(price)afterWHERE price IS NULL: every category reports 0, because the counted column isNULLon every surviving row.- Count the rows
COUNT(*)after the same filter: Music 2, Beauty 2, Toys 2, Garden 1, Automotive 1, Clothing 1.
Common follow-up questions
- How would you list every category, including those with no unpriced products, showing 0? (Tests moving the test from the filter into the aggregate so passing groups survive.)
- The team adds a second rule: every product must also carry a rating. How do you report both failures per category in one pass? (Tests counting several expectations side by side.)
- What changes if `category` itself can be `NULL`? (Tests knowing that all `NULL` categories fall into one group of their own.)