Merge request activity summary with comment counts, approval counts and review status
Company: GitLab
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
A code review tool stores merge requests, the comments left on them, and the approvals they received. For every merge request, return `mr_id`, `title`, `author`, `status`, `comment_count`, `approval_count` and `review_status`. Order by `approval_count` DESC, `comment_count` DESC, `mr_id` ASC.
`review_status` follows these rules:
- `status = 'open'` and 2 or more approvals: `'Ready to Merge'`
- `status = 'open'` and fewer than 2 approvals: `'Needs Review'`
- Otherwise: `review_status` equals `status`
```hint Watch the row count
Before you count anything, work out how many joined rows a merge request with 3 comments and 2 approvals produces.
```
### Input Table(s)
`merge_requests` (primary key `id`)
| Column | Type | Meaning |
|---|---|---|
| id | INT | Merge request identifier |
| title | VARCHAR | Merge request title |
| author | VARCHAR | User who opened the merge request |
| status | VARCHAR | `'open'`, `'merged'` or `'closed'` |
`comments` (primary key `id`)
| Column | Type | Meaning |
|---|---|---|
| id | INT | Comment identifier |
| merge_request_id | INT | References `merge_requests.id` |
| author | VARCHAR | User who wrote the comment |
`approvals` (primary key `id`)
| Column | Type | Meaning |
|---|---|---|
| id | INT | Approval identifier |
| merge_request_id | INT | References `merge_requests.id` |
| approver | VARCHAR | User who approved |
### Output Contract
- Columns, in this order: `mr_id`, `title`, `author`, `status`, `comment_count`, `approval_count`, `review_status`.
- One row per merge request, including merge requests with no comments or no approvals (their counts are `0`).
- `comment_count` is the number of rows in `comments` for the merge request, and `approval_count` is the number of rows in `approvals`.
- `review_status` is `'Ready to Merge'`, `'Needs Review'`, or a copy of `status`, using the exact spelling and capitalization above.
- Order by `approval_count` descending, then `comment_count` descending, then `mr_id` ascending.
### Example
`merge_requests`
| id | title | author | status |
|---|---|---|---|
| 1 | Add login page | alice | open |
| 2 | Fix CI cache | bob | open |
| 3 | Update README | carol | merged |
| 4 | Refactor billing | alice | closed |
| 5 | Bump dependencies | dave | open |
`comments`
| id | merge_request_id | author |
|---|---|---|
| 1 | 1 | bob |
| 2 | 1 | carol |
| 3 | 1 | dave |
| 4 | 2 | alice |
| 5 | 3 | alice |
| 6 | 3 | dave |
`approvals`
| id | merge_request_id | approver |
|---|---|---|
| 1 | 1 | bob |
| 2 | 1 | carol |
| 3 | 2 | carol |
| 4 | 3 | bob |
| 5 | 3 | dave |
Expected result:
| mr_id | title | author | status | comment_count | approval_count | review_status |
|---|---|---|---|---|---|---|
| 1 | Add login page | alice | open | 3 | 2 | Ready to Merge |
| 3 | Update README | carol | merged | 2 | 2 | merged |
| 2 | Fix CI cache | bob | open | 1 | 1 | Needs Review |
| 4 | Refactor billing | alice | closed | 0 | 0 | closed |
| 5 | Bump dependencies | dave | open | 0 | 0 | Needs Review |
Merge requests 1 and 3 both have 2 approvals, and 1 comes first because it has more comments. Merge request 3 has 2 approvals but is merged, so its `review_status` is its status. Merge requests 4 and 5 tie on both counts, so `mr_id` decides the order.
### Constraints and Clarifications
- Row counts: `merge_requests` 1 to 10,000, `comments` 0 to 100,000, `approvals` 0 to 10,000.
- `status` is one of `'open'`, `'merged'`, `'closed'` and is never NULL.
- Each `id` is unique within its table.
- Every comment and approval references an existing merge request.
- Every approval row counts once, even if the same approver appears twice on one merge request.
Overview: Write a SQL query that lists every merge request with its comment count, approval count and a review status derived from those counts. It tests left joins that keep rows with no children, avoiding fan-out when two child tables are joined at once, and deterministic ordering.