Find issues that carry both the bug and urgent labels in a many-to-many label schema
Company: GitLab
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
A project tracker stores issues and merge requests. Both can carry any number of labels from one shared `labels` table, linked through the join tables `issue_labels` and `merge_request_labels`.
Find all issues that have both the `bug` and `urgent` labels (issues with only one of them are excluded). Return `issue_id`, `issue_title`, and `labels`: a comma-separated list of the matching label names sorted alphabetically.
```hint What to count
Once each issue's labels are narrowed to the two names you care about, decide whether you should count label rows or label names per issue.
```
### Input Table(s)
Table `issues`, primary key `issue_id`:
| Column | Type | Meaning |
|---|---|---|
| issue_id | INTEGER | Unique issue id |
| issue_title | TEXT | Title of the issue |
Table `merge_requests`, primary key `merge_request_id`:
| Column | Type | Meaning |
|---|---|---|
| merge_request_id | INTEGER | Unique merge request id |
| merge_request_title | TEXT | Title of the merge request |
Table `labels`, primary key `label_id`:
| Column | Type | Meaning |
|---|---|---|
| label_id | INTEGER | Unique label id |
| name | TEXT | Label name as shown in the tracker, such as `bug` |
Table `issue_labels`, primary key `(issue_id, label_id)`:
| Column | Type | Meaning |
|---|---|---|
| issue_id | INTEGER | References `issues.issue_id` |
| label_id | INTEGER | References `labels.label_id` |
Table `merge_request_labels`, primary key `(merge_request_id, label_id)`:
| Column | Type | Meaning |
|---|---|---|
| merge_request_id | INTEGER | References `merge_requests.merge_request_id` |
| label_id | INTEGER | References `labels.label_id` |
### Output Contract
One row per qualifying issue, with exactly these columns:
| Column | Type | Meaning |
|---|---|---|
| issue_id | INTEGER | The issue |
| issue_title | TEXT | Its title |
| labels | TEXT | The matching label names in alphabetical order, joined by a comma and a space |
Only `bug` and `urgent` can match, so `labels` is `bug, urgent` for every qualifying issue. Sort the rows by `issue_id` ascending. No rounding applies.
### Example
`labels`:
| label_id | name |
|---|---|
| 1 | bug |
| 2 | urgent |
| 3 | frontend |
| 4 | docs |
| 5 | bug |
`issues`:
| issue_id | issue_title |
|---|---|
| 1 | Login page crashes on submit |
| 2 | Typo in the README |
| 3 | Payment webhook times out |
| 4 | Dark mode colors are off |
`issue_labels`:
| issue_id | label_id |
|---|---|
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 2 |
| 2 | 4 |
| 3 | 2 |
| 3 | 5 |
| 4 | 1 |
| 4 | 5 |
`merge_requests`:
| merge_request_id | merge_request_title |
|---|---|
| 2 | Retry webhook delivery |
`merge_request_labels`:
| merge_request_id | label_id |
|---|---|
| 2 | 1 |
| 2 | 2 |
Expected result:
| issue_id | issue_title | labels |
|---|---|---|
| 1 | Login page crashes on submit | bug, urgent |
| 3 | Payment webhook times out | bug, urgent |
Issue 1 qualifies, and its `frontend` label stays out of `labels`. Issue 2 has only `urgent`. Merge request 2 carries both labels, but it is a merge request, so it does not make issue 2 qualify. Issue 3 qualifies through label 5, a second label row named `bug`. Issue 4 has two `bug` label rows and no `urgent`, so it is excluded.
### Constraints and Clarifications
- `labels` has at most 100 rows. `issues` and `merge_requests` have at most 10,000 rows each.
- `(issue_id, label_id)` and `(merge_request_id, label_id)` pairs are unique.
- Label names are matched exactly as stored, in lowercase: `bug` and `urgent`.
- Two label rows can share a name (label ids 1 and 5 in the example). The condition is about names: an issue qualifies when both `bug` and `urgent` appear among its label names, and each name appears once in `labels`.
- An issue can carry other labels as well. It still qualifies, and the other labels do not appear in `labels`.
- An issue with no labels never qualifies. None of the columns used here contain NULLs.
- Only `issue_labels` decides the result. Labels attached to merge requests do not count.
Overview: Write a PostgreSQL query over an issue tracker's many-to-many label tables that returns every issue tagged with both bug and urgent, along with an alphabetical list of the matching labels. It tests joins through a link table, filtering groups after aggregation, and ordered string aggregation.
A project tracker stores issues and merge requests. Both can carry any number of labels from one shared `labels` table, linked through the join tables `issue_labels` and `merge_request_labels`.
Find all issues that have both the `bug` and `urgent` labels (issues with only one of them are excluded). Return `issue_id`, `issue_title`, and `labels`: a comma-separated list of the matching label names sorted alphabetically.
Input tables:
- `issues(issue_id INTEGER, issue_title TEXT)`, primary key `issue_id`: one row per issue with its title.
- `merge_requests(merge_request_id INTEGER, merge_request_title TEXT)`, primary key `merge_request_id`: one row per merge request with its title.
- `labels(label_id INTEGER, name TEXT)`, primary key `label_id`: the label name as shown in the tracker, such as `bug`.
- `issue_labels(issue_id INTEGER, label_id INTEGER)`, primary key `(issue_id, label_id)`: `issue_id` references `issues.issue_id` and `label_id` references `labels.label_id`.
- `merge_request_labels(merge_request_id INTEGER, label_id INTEGER)`, primary key `(merge_request_id, label_id)`: `merge_request_id` references `merge_requests.merge_request_id` and `label_id` references `labels.label_id`.
Output: one row per qualifying issue, with exactly these columns in this order: `issue_id` (INTEGER, the issue), `issue_title` (TEXT, its title) and `labels` (TEXT, the matching label names in alphabetical order, joined by a comma and a space). Only `bug` and `urgent` can match, so `labels` is `bug, urgent` for every qualifying issue. Sort the rows by `issue_id` ascending. No rounding applies.
Constraints and clarifications:
- `labels` has at most 100 rows. `issues` and `merge_requests` have at most 10,000 rows each.
- `(issue_id, label_id)` and `(merge_request_id, label_id)` pairs are unique.
- Label names are matched exactly as stored, in lowercase: `bug` and `urgent`.
- Two label rows can share a name. The condition is about names: an issue qualifies when both `bug` and `urgent` appear among its label names, and each name appears once in `labels`.
- An issue can carry other labels as well. It still qualifies, and the other labels do not appear in `labels`.
- An issue with no labels never qualifies. None of the columns used here contain NULLs.
- Only `issue_labels` decides the result. Labels attached to merge requests do not count.
Tables
issues(issue_id INTEGER, issue_title TEXT)
merge_requests(merge_request_id INTEGER, merge_request_title TEXT)
labels(label_id INTEGER, name TEXT)
issue_labels(issue_id INTEGER, label_id INTEGER)
merge_request_labels(merge_request_id INTEGER, label_id INTEGER)
Hints
- Once each issue's labels are narrowed to the two names you care about, decide whether you should count label rows or label names per issue.