The Loudest Threads
A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.
- Domain
- SQL
- Difficulty
- medium
- Seniority
- L4
- Asked in
- Yelp
Problem
Return the repo name and author of the three reviews in code_reviews that drew the most comments, most discussed first. Comment counts repeat often across the table, so when reviews tie on comments the one with the lower review_id wins the spot.
The code reviews that started a debate.
Tables
code_reviews
Columns: review_id INTEGER, repo_name TEXT, author TEXT, reviewer TEXT, status TEXT, comments INTEGER, opened_at TEXT, merged TEXT
| review_id | repo_name | author | reviewer | status | comments | opened_at | merged |
|---|---|---|---|---|---|---|---|
| 1053 | backend-api | bob | eve | changes_requested | 3 | 2026-02-02 | 2026-02-05 |
| 1106 | infra | charlie | frank | pending | 6 | 2026-03-03 | 2026-03-06 |
| 1159 | ml-pipeline | dana | grace | Approved | 9 | 2026-04-04 | 2026-04-07 |
| 1212 | mobile-app | eve | hank | PENDING | 12 | 2026-05-05 | NULL |
| 1265 | data-platform | frank | alice | merged | 15 | 2026-06-06 | 2026-06-09 |
| 1318 | frontend | grace | bob | approved | 18 | 2026-07-07 | 2026-07-10 |
| 1371 | backend-api | hank | charlie | changes_requested | 21 | 2026-08-08 | 2026-08-11 |
| 1424 | infra | alice | NULL | pending | 24 | 2026-09-09 | NULL |
| 1477 | ml-pipeline | bob | eve | Approved | 27 | 2026-01-10 | 2026-01-13 |
| 1530 | mobile-app | charlie | frank | PENDING | 30 | 2026-02-11 | 2026-02-14 |
Expected output
| repo_name | author |
|---|---|
| infra | grace |
| data-platform | frank |
| data-platform | dana |
Worked solution and explanation
What this really is
Strip off the code-review costume and this is a top-N-by-one-metric query: order every row by a single column and keep the first three. The comments total is already sitting on each row, so there is nothing to group or count. The whole problem lives in two places most candidates rush past: keeping the SELECT list to exactly the two asked-for columns, and making the third slot deterministic. Sort on comments alone and the boundary between third and fourth place is decided by whatever order the engine happened to scan, so a rerun can silently swap the last row.
Build it
Step 1: Read from code_reviews
You only need repo_name and author from code_reviews. The comments column is already a per-review total, so no aggregation is involved: this is a straight row selection.
Step 2: Order with a tiebreaker
Sort by comments descending to float the busiest reviews to the top, then add review_id as a tiebreaker so the ordering is fully determined even when two reviews share a comment count at the cutoff.
Step 3: Limit and project
Keep the first three rows with LIMIT 3 and project only the two requested columns. Returning reviewer or comments as an extra column is the easiest way to fail strict schema grading.
The solution
SELECT repo_name, author
FROM code_reviews
ORDER BY comments DESC, review_id
LIMIT 3comments DESC ranks the busiest reviews; review_id makes the third slot reproducible.
- Window function (overkill here)
- RANK() OVER (ORDER BY comments DESC) computes a rank for all 400K rows, then you filter to rank <= 3. Correct, but it ranks everything before throwing almost all of it away, and it hands you ties to reason about explicitly.
- ORDER BY plus LIMIT
- ORDER BY comments DESC,
review_idLIMIT 3 lets the engine short-circuit after three rows and reads cleanly. The right tool when you want a flat top-N, not a per-group one.
Common follow-up questions
- Now return the single most-commented review for each repo instead of the top three overall. How does your query change? (Pushes from a flat top-N to a per-group top-N, which is where the window function finally earns its place.)
- If several reviews are tied at the third-place comment count, which one does your query keep, and how do you make that choice explicit? (Tests whether the candidate can articulate the non-determinism the tiebreaker fixes.)
- What index would let this run without sorting the full table, and does the tiebreaker column need to be part of it? (Tests indexing intuition for order-then-limit queries.)