# High-Rated In-Stock Percentage

> Highly rated and in stock. A rare combo.

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

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

The merchandising lead wants a single number for the board slide: what percentage of the entire catalog is both in stock and rated 4 or above?

## Worked solution and explanation

### Why this problem exists in real interviews

Computing a percentage of products meeting two conditions tests conditional aggregation. The interviewer checks whether you can compute a ratio using CASE inside AVG.

---

### Break down the requirements

#### Step 1: Compute the percentage

Use `AVG(CASE WHEN in_stock = 1 AND rating >= 4.0 THEN 1.0 ELSE 0.0 END) * 100` for the combined percentage.

---

### The solution

**Conditional aggregation for compound percentage**

```sql
SELECT ROUND(
    AVG(CASE WHEN in_stock = 1 AND rating >= 4.0 THEN 1.0 ELSE 0.0 END) * 100, 2
) AS pct_in_stock_high_rated
FROM products
```

> **Cost Analysis**
>
> Single-pass scan with a CASE aggregate. Trivially cheap.

> **Interviewers Watch For**
>
> The interviewer checks whether you layer the two conditions (in_stock AND high_rated) correctly in a single CASE expression.

> **Common Pitfall**
>
> Computing separate percentages and multiplying them is wrong. The conditions must be combined in a single CASE.

---

## Common follow-up questions

- How would you compute this per category? _(Tests adding GROUP BY category.)_
- What if rating is on a 1-10 scale? _(Tests adjusting the threshold.)_
- How would you find categories below 50%? _(Tests GROUP BY with HAVING.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/high_rated_in_stock_percentage)
- [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.