# Most Commented Code Review

> The code review that started a debate.

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

Domain: SQL · Difficulty: medium · Seniority: L4

## Problem

Which code review attracted the most comments? Return the repo name and review author for that single review.

## Worked solution and explanation

### Why this problem exists in real interviews

Against the code_reviews table, top-N selection on repo_name values is the key operation. Interviewers favor this in mid-level screens because it exposes whether candidates handle ties, NULLs, and ordering edge cases correctly.

> **Trick to Solving**
>
> Read the prompt carefully for implicit constraints. The phrase structure hints at the grain of the output: what each row represents.
> 
> 1. Identify the output grain from the prompt (one row per what?)
> 2. Work backward from the desired output columns
> 3. Build the query inside-out: innermost subquery first, then layer on filters and aggregates

---

### Break down the requirements

#### Step 1: Read from `code_reviews`

The query targets `code_reviews` with 8 columns. Identify which columns are needed for the output.

#### Step 2: Order and limit the output

Sort by the target metric and apply `LIMIT` to return the requested number of rows. Ensure the sort is deterministic to produce reproducible results.

#### Step 3: Return the result set

Select the required columns with any necessary aliasing or formatting.

---

### The solution

**Single-row maximum via ORDER BY LIMIT**

```sql
SELECT repo_name, author
FROM code_reviews
ORDER BY comments DESC
LIMIT 1
```

> **Cost Analysis**
>
> The query scans 400K rows from `code_reviews`. CTEs in most engines are optimization fences. For production workloads, consider inlining or materializing the intermediate results.

> **Interviewers Watch For**
>
> Breaking complex logic into named CTEs shows the interviewer you prioritize readability and debuggability.

> **Common Pitfall**
>
> Returning more columns than the prompt asks for can trigger a "wrong schema" failure in automated grading. Match the output specification exactly.

---

## Common follow-up questions

- What happens to your result if code_reviews.merged contains NULLs for some rows? _(Tests whether the candidate accounts for NULL behavior in aggregates and comparisons on merged.)_
- How would you verify that your aggregation on code_reviews.review_id is not double-counting due to duplicate rows? _(Tests data quality awareness and deduplication strategies.)_
- With millions of distinct values in code_reviews.review_id, what index strategy would you use to keep this query performant? _(Tests indexing knowledge specific to high-cardinality columns like review_id.)_

## Related

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