# The Sweet Spot

> Cheap and highly rated. A rare combination.

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

Domain: SQL · Difficulty: medium · Seniority: L3

## Problem

A budget-conscious shopper is comparing electronics that are worth buying: in stock and rated at least 4.0. List those products cheapest first, showing just the name and price.

## Worked solution and explanation

### A filter problem wearing a shopping-list costume

Strip the retail framing and this is a three-predicate filter with a sort on top. The skill being probed is whether you can stack independent conditions without letting a cheaper decoy leak through. The seed is built to punish sloppiness: Yoga Mat at 5.99 is the cheapest thing in the table but is not electronics, Budget Headphones at 8.50 is electronics but rated 3.2, and Screen Cleaner at 6.75 is a highly rated electronic that is out of stock. Drop any one of the three predicates and one of those decoys jumps to the top of your list. The real cheapest qualifier is HDMI Cable at 9.99.

---

### The three predicates and the decoys they kill

#### Step 1: Stack the filters

All three conditions live in one WHERE: rating >= 4.0 removes Budget Headphones, in_stock = 1 removes Screen Cleaner and Premium Monitor, and the category test removes Yoga Mat and Cookbook. Each predicate is pulling its weight against a specific decoy row, which is exactly why forgetting one is visible in the output.

#### Step 2: Match the substring, not the exact string

The word is containing, not equals. Phone Stand sits under Consumer Electronics, so category = 'Electronics' would silently drop it. category LIKE '%Electronics%' keeps any category with the word anywhere in it. This is the single most common miss on this shape.

#### Step 3: Sort cheapest first, project two columns

ORDER BY price ASC puts the cheapest qualifier first, and the projection is only product_name and price. No LIMIT here: the ask is the full ranked list of qualifiers, cheapest first, not just the top one.

**Three predicates, sorted cheapest first**

```sql
SELECT product_name, price
FROM products
WHERE rating >= 4.0
  AND in_stock = 1
  AND category LIKE '%Electronics%'
ORDER BY price ASC
```

> **Interviewers watch for**
>
> The tell is whether the candidate writes category LIKE '%Electronics%' or category = 'Electronics'. The prompt says containing, and the data has a Consumer Electronics row waiting to expose the exact-match version. A candidate who asks about that category before writing the query is signalling seniority.

> **Common pitfall**
>
> The classic error is anchoring on price alone and forgetting a predicate. Skip the rating filter and Budget Headphones (8.50) tops the list; skip in_stock and Screen Cleaner (6.75) wins. The cheapest row is only correct once all three filters have run.

> **Performance insight**
>
> At 8K rows this is an instant full scan. If the table grew to millions, a composite index on (category, rating, in_stock, price) would let the planner seek the qualifying band and read it already ordered, avoiding a sort. The leading-wildcard LIKE cannot use that index prefix, so at real scale you would push category matching into a normalized flag or a full-text path.

---

## Common follow-up questions

- If the team only wanted the single cheapest qualifier, how would you handle two products tied at the lowest price? _(Tests FETCH FIRST WITH TIES or a window function instead of a bare LIMIT.)_
- What if categories were stored inconsistently, like ELECTRONICS and electronics? _(Tests LOWER() or a case-insensitive collation for portable matching.)_
- How does your filter behave for products whose rating is NULL? _(Tests handling of the 3 percent null rating fraction in the real table.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_sweet_spot)
- [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). DataDriven is the data engineering interview community. Live code execution in SQL, Python, and Spark sandboxes. Every feature is open to every member.