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.

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 Tables `merge_requests` (primary key `id`) | Column | Type | Meaning | |---|---|---| | id | INTEGER | 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 | INTEGER | Comment identifier | | merge_request_id | INTEGER | References `merge_requests.id` | | author | VARCHAR | User who wrote the comment | `approvals` (primary key `id`) | Column | Type | Meaning | |---|---|---| | id | INTEGER | Approval identifier | | merge_request_id | INTEGER | 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`. - `mr_id` is `merge_requests.id`, and `title`, `author` and `status` are the merge request's own columns from `merge_requests`. - 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. ### 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.

Tables

merge_requests(id INTEGER, title VARCHAR(200), author VARCHAR(100), status VARCHAR(20))

comments(id INTEGER, merge_request_id INTEGER, author VARCHAR(100))

approvals(id INTEGER, merge_request_id INTEGER, approver VARCHAR(100))

Hints

  1. Watch the row count. Before you count anything, work out how many joined rows a merge request with 3 comments and 2 approvals produces.
  2. A merge request with no comments or no approvals still gets a row, and its missing count is 0.
GitLabData ScientistmediumData Manipulation (SQL/Python)

Merge Request Review Status

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 Tables

merge_requests (primary key id)

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

comments (primary key id)

ColumnTypeMeaning
idINTEGERComment identifier
merge_request_idINTEGERReferences merge_requests.id
authorVARCHARUser who wrote the comment

approvals (primary key id)

ColumnTypeMeaning
idINTEGERApproval identifier
merge_request_idINTEGERReferences merge_requests.id
approverVARCHARUser who approved

Output Contract

  • Columns, in this order: mr_id, title, author, status, comment_count, approval_count, review_status.
  • mr_id is merge_requests.id, and title, author and status are the merge request's own columns from merge_requests.
  • 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.

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.

Expected Output

Tables

merge_requestscommentsapprovals
merge_requests
ColumnType
idINTEGER
titleVARCHAR(200)
authorVARCHAR(100)
statusVARCHAR(20)
comments
ColumnType
idINTEGER
merge_request_idINTEGER
authorVARCHAR(100)
approvals
ColumnType
idINTEGER
merge_request_idINTEGER
approverVARCHAR(100)

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions