Quick Overview

Write a SQL query that counts monthly active users, where a user is active in a month if they made at least one commit, push or merge request. It tests filtering before aggregation, counting distinct users per group and formatting dates as year-month labels that sort correctly.

Monthly active users from commit, push and merge request activity

Company: GitLab

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Compute Monthly Active Users. A user is active in a month if they performed at least one `'commit'`, `'push'` or `'merge_request'` activity that month. Other activity types are ignored. ```hint Count people, not rows A single user can have several qualifying activities in the same month. Make sure each user adds at most 1 to that month's number. ``` ### Input Table(s) `user_activities` (primary key `id`) | Column | Type | Meaning | |---|---|---| | id | INT | Activity identifier | | user_id | INT | User who performed the activity, NOT NULL | | activity_type | VARCHAR(50) | Kind of activity, such as `'commit'` or `'login'`, NOT NULL | | activity_date | DATE | Day of the activity, NOT NULL | ### Output Contract - Columns, in this order: `month` (text formatted `YYYY-MM`) and `active_users` (count of distinct users). - One row per month that has at least one qualifying activity. - Order by `month` ascending. ### Example `user_activities` | id | user_id | activity_type | activity_date | |---|---|---|---| | 1 | 101 | commit | 2024-01-05 | | 2 | 102 | push | 2024-01-10 | | 3 | 101 | push | 2024-01-15 | | 4 | 103 | merge_request | 2024-02-01 | | 5 | 101 | commit | 2024-02-14 | | 6 | 104 | login | 2024-02-20 | | 7 | 102 | commit | 2024-02-25 | | 8 | 105 | browse | 2024-01-18 | Expected result: | month | active_users | |---|---| | 2024-01 | 2 | | 2024-02 | 3 | In January, users 101 and 102 qualify. User 101 has two qualifying rows but counts once, and user 105 only browsed. In February, users 101, 102 and 103 qualify, and user 104 only logged in. ### Constraints and Clarifications - The table has 1 to 10,000 rows. - `activity_type` can be any string. Only the exact values `'commit'`, `'push'` and `'merge_request'` qualify. - A user can have multiple activities on the same day or in the same month. - The same calendar month in different years is a different month (`2024-01` and `2025-01` are separate rows). - A month whose activities are all of other types does not appear in the output.

Overview: Write a SQL query that counts monthly active users, where a user is active in a month if they made at least one commit, push or merge request. It tests filtering before aggregation, counting distinct users per group and formatting dates as year-month labels that sort correctly.

Compute Monthly Active Users from the `user_activities` table. A user is active in a month if they performed at least one activity whose `activity_type` is exactly `'commit'`, `'push'` or `'merge_request'` in that month. Other activity types are ignored. Table `user_activities` (primary key `id`): - `id` INTEGER: activity identifier - `user_id` INTEGER NOT NULL: user who performed the activity - `activity_type` VARCHAR(50) NOT NULL: kind of activity, such as `'commit'` or `'login'` - `activity_date` DATE NOT NULL: day of the activity Return the columns `month` (text formatted `YYYY-MM`) and `active_users` (the number of distinct users with at least one qualifying activity in that month), in that order. Return one row per month that has at least one qualifying activity, ordered by `month` ascending. Clarifications: - The table has 1 to 10,000 rows. - `activity_type` can be any string. Only the exact values `'commit'`, `'push'` and `'merge_request'` qualify. - A user can have multiple activities on the same day or in the same month, and each user adds at most 1 to a month's count. - The same calendar month in different years is a different month (`2024-01` and `2025-01` are separate rows). - A month whose activities are all of other types does not appear in the output.

Tables

user_activities(id INTEGER, user_id INTEGER, activity_type VARCHAR(50), activity_date DATE)

Hints

  1. Count people, not rows. A single user can have several qualifying activities in the same month, and each user adds at most 1 to that month's number.
  2. The same calendar month in different years is a different month, so 2024-01 and 2025-01 are separate rows.
GitLabData ScientisteasyData Manipulation (SQL/Python)

Monthly Active Users

Compute Monthly Active Users from the user_activities table. A user is active in a month if they performed at least one activity whose activity_type is exactly 'commit', 'push' or 'merge_request' in that month. Other activity types are ignored.

Table user_activities (primary key id):

  • id INTEGER: activity identifier
  • user_id INTEGER NOT NULL: user who performed the activity
  • activity_type VARCHAR(50) NOT NULL: kind of activity, such as 'commit' or 'login'
  • activity_date DATE NOT NULL: day of the activity

Return the columns month (text formatted YYYY-MM) and active_users (the number of distinct users with at least one qualifying activity in that month), in that order. Return one row per month that has at least one qualifying activity, ordered by month ascending.

Clarifications:

  • The table has 1 to 10,000 rows.
  • activity_type can be any string. Only the exact values 'commit', 'push' and 'merge_request' qualify.
  • A user can have multiple activities on the same day or in the same month, and each user adds at most 1 to a month's count.
  • The same calendar month in different years is a different month (2024-01 and 2025-01 are separate rows).
  • A month whose activities are all of other types does not appear in the output.

Expected Output

Tables

user_activities
user_activities
ColumnType
idINTEGER
user_idINTEGER
activity_typeVARCHAR(50)
activity_dateDATE

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