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

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 6 rows
categoryunpriced_productsavg_rating
Automotive11.9
Beauty22.35
Clothing12.3
Garden11.6
Music22.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.

Unpriced products and their average rating per category
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) after WHERE price IS NULL: every category reports 0, because the counted column is NULL on 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