# Average Review Comments by Author

> Some authors get more feedback than others.

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

Domain: SQL · Difficulty: medium · Seniority: L3

## Problem

The engineering manager noticed that some long-tenured contributors still get heavy review feedback while newer ones sail through. For each author, pull the average number of comments on their code reviews alongside the date of their earliest commit. Present results starting from the most tenured author.

## Worked solution and explanation

### Why this problem exists in real interviews

By forcing grouped aggregation on `code_reviews`, `repo_commits`, this question separates candidates who understand how `repo_name`, `author`, `reviewer` behave under aggregation from those who guess at the GROUP BY clause.

---

### Break down the requirements

#### Step 1: Join `code_reviews` with `repo_commits`

Use `JOIN` on `author` to link fact rows to the dimension table. INNER JOIN keeps only matched rows.

#### Step 2: Group by `repo_name`

`GROUP BY b.repo_name` produces one row per distinct value of the grouping column.

#### Step 3: Aggregate with AVG

`AVG(comments)` computes the metric at the grouped grain.

#### Step 4: Order by the metric

Sort descending to surface the top values first.

---

### The solution

**Group-aggregate for average review comments author**

```sql
SELECT
    b.repo_name,
    AVG(comments) AS avg_comments
FROM code_reviews a
JOIN repo_commits b ON a.author = b.author
GROUP BY b.repo_name
ORDER BY avg_comments DESC
```

> **Cost Analysis**
>
> The main table has 500K 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. Join type selection (INNER vs LEFT) reveals whether the candidate read the requirements carefully.

> **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 `merged` column in `code_reviews` has roughly 10% 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.)_
- If `code_reviews` and `repo_commits` have a one-to-many relationship, how does that affect the COUNT in your GROUP BY? _(Tests understanding of fan-out: joining before grouping can inflate counts.)_
- `commit_id` in `repo_commits` has ~2M distinct values. What index strategy keeps your query from doing a full table scan? _(Tests whether the candidate can design indexes for high-cardinality columns and understands selectivity.)_
- 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_review_comments_by_author)
- [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.