Quick Overview

Write a SQL query that joins issues to their projects and builds a readable display key such as Alpha-1 from the project name and the per-project issue number. It tests joins, concatenating text with integers, and ordering on a composite unique key.

Build project-scoped issue display keys from global ID and IID

Company: GitLab

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Each issue in a project tracker has two identifiers. The global `id` is the primary key across all projects. The `iid` is scoped to a project: numbering starts at 1 in every project, so two different projects can each have an issue with `iid = 1`. Users see the project-scoped form, such as `Alpha-1`. Write a query that returns every issue with its `id`, `project_id`, `iid`, `project_name`, `title`, and a `display_key` built as the project name, a hyphen, and the `iid` (for example `Alpha-1`). Order the result by `project_id`, then `iid`, both ascending. ```hint Building the key `iid` is an integer and `name` is text. Check how your query turns the number into part of a string, and what the concatenation operator you pick does in your SQL dialect. ``` ### Input Table(s) `projects` | Column | Type | Meaning | |---|---|---| | id | INT | Project id. Primary key. | | name | VARCHAR(100) | Project name. NOT NULL. | `issues` | Column | Type | Meaning | |---|---|---| | id | INT | Global issue id. Primary key. | | project_id | INT | Project the issue belongs to. NOT NULL, foreign key to `projects.id`. | | iid | INT | Issue number within its project. NOT NULL. | | title | VARCHAR(255) | Issue title. NOT NULL. | | created_at | TIMESTAMP | When the issue was created. NOT NULL. | Keys: `projects.id` and `issues.id` are primary keys. `(issues.project_id, issues.iid)` is unique. `issues.project_id` references `projects.id`. ### Output Contract - Columns, in this order: `id`, `project_id`, `iid`, `project_name`, `title`, `display_key`. - `id`, `project_id`, `iid` and `title` come from `issues`. `project_name` is `projects.name`. - `display_key` is text: the project name exactly as stored, then `-`, then `iid` written as a plain decimal number with no padding (`Alpha-1`, `Alpha-12`). - One row per issue. - Order by `project_id` ascending, then `iid` ascending. That pair is unique, so the order is fully determined. - No rounding applies. ### Example `projects` | id | name | |---|---| | 1 | Alpha | | 2 | Beta | | 3 | Gamma | `issues` | id | project_id | iid | title | created_at | |---|---|---|---|---| | 101 | 2 | 1 | Login page returns 500 | 2024-03-01 09:00:00 | | 102 | 1 | 1 | Add CSV export | 2024-03-01 10:15:00 | | 103 | 1 | 2 | Fix typo in README | 2024-03-02 08:30:00 | | 104 | 2 | 2 | Rate limit webhook calls | 2024-03-03 14:00:00 | Expected output: | id | project_id | iid | project_name | title | display_key | |---|---|---|---|---|---| | 102 | 1 | 1 | Alpha | Add CSV export | Alpha-1 | | 103 | 1 | 2 | Alpha | Fix typo in README | Alpha-2 | | 101 | 2 | 1 | Beta | Login page returns 500 | Beta-1 | | 104 | 2 | 2 | Beta | Rate limit webhook calls | Beta-2 | Issue 101 has the smallest global `id` but comes third, because the order is by project and then by project-scoped number. Gamma has no issues, so it produces no row. ### Constraints and Clarifications - `issues` has at most 10,000 rows and `projects` at most 1,000. - `iid` values are positive integers starting from 1 within each project. - `(project_id, iid)` is unique, so no two rows tie on the sort key. - Every issue belongs to an existing project, and no column in either table is NULL. - Projects without issues do not appear in the output.

Overview: Write a SQL query that joins issues to their projects and builds a readable display key such as Alpha-1 from the project name and the per-project issue number. It tests joins, concatenating text with integers, and ordering on a composite unique key.

Each issue in a project tracker has two identifiers. The global `id` is the primary key across all projects. The `iid` is scoped to a project: numbering starts at 1 in every project, so two different projects can each have an issue with `iid = 1`. Users see the project-scoped form, such as `Alpha-1`. Tables: - `projects(id INTEGER PRIMARY KEY, name VARCHAR(100) NOT NULL)`: one row per project, where `name` is the project name. - `issues(id INTEGER PRIMARY KEY, project_id INTEGER NOT NULL, iid INTEGER NOT NULL, title VARCHAR(255) NOT NULL, created_at TIMESTAMP NOT NULL)`: `id` is the global issue id, `project_id` is the project the issue belongs to (it references `projects.id`), `iid` is the issue number within its project, `title` is the issue title, and `created_at` is when the issue was created. `(project_id, iid)` is unique. Write a query that returns every issue with its `id`, `project_id`, `iid`, `project_name`, `title`, and a `display_key` built as the project name, a hyphen, and the `iid` (for example `Alpha-1`). Output contract: - Columns, in this order: `id`, `project_id`, `iid`, `project_name`, `title`, `display_key`. - `id`, `project_id`, `iid` and `title` come from `issues`. `project_name` is `projects.name`. - `display_key` is text: the project name exactly as stored, then `-`, then `iid` written as a plain decimal number with no padding (`Alpha-1`, `Alpha-12`). - One row per issue. - Order by `project_id` ascending, then `iid` ascending. That pair is unique, so the order is fully determined. - No rounding applies. Constraints and clarifications: - `issues` has at most 10,000 rows and `projects` at most 1,000. - `iid` values are positive integers starting from 1 within each project. - Every issue belongs to an existing project, and no column in either table is NULL. - Projects without issues do not appear in the output.

Tables

projects(id INTEGER, name VARCHAR(100))

issues(id INTEGER, project_id INTEGER, iid INTEGER, title VARCHAR(255), created_at TIMESTAMP)

Hints

  1. `iid` is an integer and `name` is text. Check how your query turns the number into part of a string, and what the concatenation operator you pick does in your SQL dialect.
  2. Every issue has a matching project, and a project with no issues produces no row.
GitLabData ScientisteasyData Manipulation (SQL/Python)

Project-Scoped Issue Display Keys

Each issue in a project tracker has two identifiers. The global id is the primary key across all projects. The iid is scoped to a project: numbering starts at 1 in every project, so two different projects can each have an issue with iid = 1. Users see the project-scoped form, such as Alpha-1.

Tables:

  • projects(id INTEGER PRIMARY KEY, name VARCHAR(100) NOT NULL): one row per project, where name is the project name.
  • issues(id INTEGER PRIMARY KEY, project_id INTEGER NOT NULL, iid INTEGER NOT NULL, title VARCHAR(255) NOT NULL, created_at TIMESTAMP NOT NULL): id is the global issue id, project_id is the project the issue belongs to (it references projects.id), iid is the issue number within its project, title is the issue title, and created_at is when the issue was created. (project_id, iid) is unique.

Write a query that returns every issue with its id, project_id, iid, project_name, title, and a display_key built as the project name, a hyphen, and the iid (for example Alpha-1).

Output contract:

  • Columns, in this order: id, project_id, iid, project_name, title, display_key.
  • id, project_id, iid and title come from issues. project_name is projects.name.
  • display_key is text: the project name exactly as stored, then -, then iid written as a plain decimal number with no padding (Alpha-1, Alpha-12).
  • One row per issue.
  • Order by project_id ascending, then iid ascending. That pair is unique, so the order is fully determined.
  • No rounding applies.

Constraints and clarifications:

  • issues has at most 10,000 rows and projects at most 1,000.
  • iid values are positive integers starting from 1 within each project.
  • Every issue belongs to an existing project, and no column in either table is NULL.
  • Projects without issues do not appear in the output.

Expected Output

Tables

projectsissues
projects
ColumnType
idINTEGER
nameVARCHAR(100)
issues
ColumnType
idINTEGER
project_idINTEGER
iidINTEGER
titleVARCHAR(255)
created_atTIMESTAMP

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