# Ghost Products

> Listed but never sold. The shelves collect dust.

Canonical URL: <https://datadriven.io/problems/ghost_products>

Domain: SQL · Difficulty: medium · Seniority: L5

## Problem

Merchandising is hunting for dead inventory that has never sold. Find every product in products that has no matching row in transactions. Return the product_name for each.

## Worked solution and explanation

### Why this problem exists in real interviews

Finding products with no sales tests the anti-join pattern. The interviewer checks whether you use LEFT JOIN + IS NULL, NOT EXISTS, or NOT IN, and understand the NULL caveat with NOT IN.

---

### Break down the requirements

#### Step 1: Left join products to transactions

`LEFT JOIN transactions ON products.product_id = transactions.product_id` preserves all products.

#### Step 2: Filter for unmatched products

`WHERE transactions.product_id IS NULL` isolates products that never sold.

---

### The solution

**Anti-join for unsold products**

```sql
SELECT p.product_id, p.product_name, p.category, p.price
FROM products p
LEFT JOIN transactions t ON p.product_id = t.product_id
WHERE t.product_id IS NULL
ORDER BY p.product_id
```

> **Cost Analysis**
>
> Standard anti-join. An index on `transactions(product_id)` makes the join efficient.

> **Interviewers Watch For**
>
> The interviewer watches for the anti-join pattern choice. NOT IN with NULLs in the subquery is a classic trap.

> **Common Pitfall**
>
> `NOT IN (SELECT product_id FROM transactions)` fails if any product_id in transactions is NULL. Use LEFT JOIN + IS NULL or NOT EXISTS.

---

## Common follow-up questions

- Why does NOT IN fail with NULLs in the subquery? _(Tests three-valued logic understanding.)_
- How would you find products added in the last 30 days with no sales? _(Tests adding a date filter.)_
- Which anti-join pattern is fastest? _(Tests LEFT JOIN vs. NOT EXISTS performance.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/ghost_products)
- [SQL Interview Questions](https://datadriven.io/sql-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.