# Average Rating by Category

> Category ratings. Some shine, some don't.

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

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

Before the next product roadmap meeting, the PM wants to know which categories customers love and which ones need attention. Show the average product rating for each category.

## Worked solution and explanation

### Why this problem exists in real interviews

By forcing grouped aggregation on `products`, this question separates candidates who understand how `product_name`, `category`, `price` behave under aggregation from those who guess at the GROUP BY clause.

---

### Break down the requirements

#### Step 1: Group by `category`

`GROUP BY` at the correct grain produces one row per group.

#### Step 2: Compute `AVG(rating)`

The AVG function computes the avg per group.

#### Step 3: Order by the metric

Sort by `avg_rating` desc for readability.

---

### The solution

**Group-aggregate for average rating category**

```sql
SELECT
    category,
    AVG(rating) AS avg_rating
FROM products
GROUP BY category
ORDER BY avg_rating DESC
```

> **Cost Analysis**
>
> The main table has 15K rows. The GROUP BY reduces the row count early, keeping downstream operations cheap.

> **Interviewers Watch For**
>
> Strong candidates state the correct `GROUP BY` grain before writing any SQL, showing they think about the output shape first.

> **Common Pitfall**
>
> Selecting a non-aggregated column without including it in `GROUP BY` is the most common error. Some engines reject it; others silently return arbitrary values.

---

## Common follow-up questions

- The `rating` column in `products` has roughly 5% NULLs. How does your query handle those rows, and would the result change if NULLs were replaced with zeros? _(Tests whether the candidate understands how NULLs propagate through aggregation functions and whether their WHERE/JOIN conditions implicitly filter them out.)_
- Your GROUP BY aggregates `product_id` from `products`. If two groups have the same aggregate value, how is the output ordered, and is that deterministic? _(Tests awareness that ORDER BY on a non-unique value produces non-deterministic row order without a tiebreaker.)_
- The `category` column in `products` has a zipf distribution, meaning a few values dominate. How does that skew affect your query plan and parallelism? _(Tests understanding of data skew: the optimizer may choose a bad plan when histogram statistics are stale.)_
- Could you express this same logic as a single query without CTEs or subqueries? What readability trade-off does that introduce? _(Tests whether the candidate can flatten nested logic and understands when decomposition aids maintainability.)_

## Related

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