Ghost Products
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
- L5
Problem
Merchandising is hunting for dead inventory that has never sold. Return the product_name of every product in products with no matching row in transactions, walking the list alphabetically from A to Z so the team can check each one off in order.
Listed but never sold. The shelves collect dust.
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 |
transactions
Columns: transaction_id INTEGER, user_id INTEGER, product_id INTEGER, quantity INTEGER, total_amount REAL, transaction_date DATE
| transaction_id | user_id | product_id | quantity | total_amount | transaction_date |
|---|---|---|---|---|---|
| 1067 | 197 | 1001 | 2 | 23.46 | 2026-02-02 |
| 1134 | 294 | 1050 | 3 | 36.93 | 2026-03-03 |
| 1201 | 391 | 1099 | 4 | 50.4 | 2026-04-04 |
| 1268 | 488 | 1148 | 5 | 63.87 | 2026-05-05 |
| 1335 | 585 | 1197 | 0 | 0 | 2026-06-06 |
| 1402 | 682 | 1246 | 2 | 90.81 | 2026-07-07 |
| 1469 | 779 | 1295 | 3 | 104.28 | 2026-08-08 |
| 1536 | 876 | 1344 | 4 | 117.75 | 2026-09-09 |
| 1603 | 973 | 1393 | 5 | -45 | 2026-01-10 |
| 1670 | 1070 | 1442 | 1 | 144.69 | 2026-02-11 |
Expected output
| product_name |
|---|
| Basic Device 11X |
| Basic Set v10 |
| Basic Set v100 |
| Basic Set v20 |
| Basic Set v30 |
Worked solution and explanation
Why this problem exists in real interviews
This is a set difference dressed up as a merchandising report: the whole catalog minus every product that ever sold. Anyone can join products to transactions. What gets checked is whether you can make the join report absence. An inner join needs a matching sale to emit a row, so a never-sold product can never reach the output. Merchandising ends up liquidating its best sellers while the dead stock stays on the shelf.
The second trap is quieter. NOT IN (SELECT product_id FROM transactions) reads exactly like the question, but 18 of the seed's transactions rows carry a NULL product_id. A single NULL in that list makes the predicate unknown for every product, so the query runs cleanly and returns nothing.
Break down the requirements
Step 1: Keep every product, sold or not
LEFT JOIN transactions t ON p.product_id = t.product_id keeps each row of products. A product with sales fans out to one row per sale. A product with none keeps a single row whose t columns are all NULL.
Step 2: Test the join key for NULL
WHERE t.product_id IS NULL keeps only the rows the join could not match. Test the key you joined on, because a matched row always has it filled: a NULL key never equals anything, so t.product_id is empty only when no sale exists.
Step 3: Sort for the checklist
The team works the list from A to Z, so finish with ORDER BY p.product_name ascending. You do not need DISTINCT: each unmatched product kept exactly one row, so the filter already leaves one line per unsold product.
The solution
SELECT p.product_name
FROM products p
LEFT JOIN transactions t ON p.product_id = t.product_id
WHERE t.product_id IS NULL
ORDER BY p.product_nameThe NULL on the t side is the evidence that a product never sold.
- Presence (inner join)
JOIN transactions t ON p.product_id = t.product_idemits a row only where a sale exists, so every product it returns has sold.- Absence (left join plus null test)
LEFT JOINplusWHERE t.product_id IS NULLkeeps the rows the join failed to match: the 109 products with no sale on record.
Common follow-up questions
- Why does `NOT IN` fail when the subquery returns a `NULL`? (Tests three-valued logic: `x <> NULL` is unknown, never true.)
- How would you find products added in the last 30 days with no sales? (Tests adding a date filter without turning the outer join into an inner one.)
- Which anti-join pattern is fastest? (Tests reading the plan: `LEFT JOIN ... IS NULL` and `NOT EXISTS` usually compile to the same anti-join.)