The Merit Circle

A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
medium
Seniority
L3
Asked in
Google

Problem

A quality team is reviewing the products customers rate worst, reading the rating column that already holds each product's average; products still waiting on their first rating are left out of the review. Return the ten lowest-rated products, weakest first, and when two share the same average settle the order alphabetically by product name so the earlier name claims the last spot.

Ten seats at the top, and the ratings decide who sits.

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
product_namerating
Deluxe Widget v211
Deluxe Widget v611
Pro Unit 33X1.1
Pro Unit 73X1.1
Classic System 66X1.2

Worked solution and explanation

What this problem is really testing

This is a bottom-N leaderboard where the two things people forget are exactly what the grader checks: the NULL filter and the tie-break. Because the sort is ascending, the ~5% of products with a NULL rating sort to the very TOP in SQLite (NULL ranks below every real value), so skipping WHERE rating IS NOT NULL fills your ten slots with products that have no rating at all. And with only ~50 distinct rating values across 15,000 rows, the bottom of the scale is one big tied block; ORDER BY rating ASC alone leaves which ten surface, and in what order, up to the engine. The other tell is the schema: rating is ALREADY a per-product average, so anyone reaching for AVG and GROUP BY wrote SQL before reading the table.


Break down the requirements

Step 1: Read the schema before you aggregate

The prose says the rating column already reflects each product's average, so there is nothing to aggregate: no AVG, no GROUP BY. Reading that one clause is half the exercise.

Step 2: Keep unrated products out

About 5% of ratings are NULL, and in ascending order SQLite sorts those NULL rows FIRST, right where you are trying to read the ten worst real ratings. Drop them with WHERE rating IS NOT NULL so an unrated product cannot occupy a slot. Note that rating = NULL never works: it returns UNKNOWN for every row.

Step 3: Sort ascending, break ties, then cut to ten

Order by rating ASC to put the worst-rated products first, add product_name ASC as an explicit tie-break so the tied low block is deterministic, project product_name and rating, then LIMIT 10.


The solution

Filter, sort ascending with tie-break, limit
SELECT product_name, rating
FROM products
WHERE rating IS NOT NULL
ORDER BY rating ASC, product_name LIMIT 10;

Common follow-up questions