Find issues that carry both the bug and urgent labels in a many-to-many label schema

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

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.

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

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 Table(s)

Table issues, primary key issue_id:

ColumnTypeMeaning
issue_idINTEGERUnique issue id
issue_titleTEXTTitle of the issue

Table merge_requests, primary key merge_request_id:

ColumnTypeMeaning
merge_request_idINTEGERUnique merge request id
merge_request_titleTEXTTitle of the merge request

Table labels, primary key label_id:

ColumnTypeMeaning
label_idINTEGERUnique label id
nameTEXTLabel name as shown in the tracker, such as bug

Table issue_labels, primary key (issue_id, label_id):

ColumnTypeMeaning
issue_idINTEGERReferences issues.issue_id
label_idINTEGERReferences labels.label_id

Table merge_request_labels, primary key (merge_request_id, label_id):

ColumnTypeMeaning
merge_request_idINTEGERReferences merge_requests.merge_request_id
label_idINTEGERReferences labels.label_id

Output Contract

One row per qualifying issue, with exactly these columns:

ColumnTypeMeaning
issue_idINTEGERThe issue
issue_titleTEXTIts title
labelsTEXTThe 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_idname
1bug
2urgent
3frontend
4docs
5bug

issues:

issue_idissue_title
1Login page crashes on submit
2Typo in the README
3Payment webhook times out
4Dark mode colors are off

issue_labels:

issue_idlabel_id
11
12
13
22
24
32
35
41
45

merge_requests:

merge_request_idmerge_request_title
2Retry webhook delivery

merge_request_labels:

merge_request_idlabel_id
21
22

Expected result:

issue_idissue_titlelabels
1Login page crashes on submitbug, urgent
3Payment webhook times outbug, 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.