Merge request activity summary with comment counts, approval counts and review status

Quick 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.

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.

GitLab logo
GitLab
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

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

Input Table(s)

merge_requests (primary key id)

ColumnTypeMeaning
idINTMerge request identifier
titleVARCHARMerge request title
authorVARCHARUser who opened the merge request
statusVARCHAR'open', 'merged' or 'closed'

comments (primary key id)

ColumnTypeMeaning
idINTComment identifier
merge_request_idINTReferences merge_requests.id
authorVARCHARUser who wrote the comment

approvals (primary key id)

ColumnTypeMeaning
idINTApproval identifier
merge_request_idINTReferences merge_requests.id
approverVARCHARUser 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

idtitleauthorstatus
1Add login pagealiceopen
2Fix CI cachebobopen
3Update READMEcarolmerged
4Refactor billingaliceclosed
5Bump dependenciesdaveopen

comments

idmerge_request_idauthor
11bob
21carol
31dave
42alice
53alice
63dave

approvals

idmerge_request_idapprover
11bob
21carol
32carol
43bob
53dave

Expected result:

mr_idtitleauthorstatuscomment_countapproval_countreview_status
1Add login pagealiceopen32Ready to Merge
3Update READMEcarolmerged22merged
2Fix CI cachebobopen11Needs Review
4Refactor billingaliceclosed00closed
5Bump dependenciesdaveopen00Needs 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.