Find the top 5 committers to a repository in a January 2024 date window
Quick Overview
Write a SQL query that finds the five users with the most commits to one repository between January 1 and January 30, 2024, breaking ties by user id. It tests date filtering with a half-open range, grouping and counting per user, and deterministic top-N ordering.
Find the top 5 committers to a repository in a January 2024 date window
Company: GitHub
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Find the top 5 users by number of commits in `repository_id = 1` with `commit_date >= '2024-01-01'` and `commit_date < '2024-01-31'`. Return `user_id`, `username` and `commit_count`.
```hint Read the bounds literally
Check exactly which calendar days the two date conditions include, and what should happen to users tied at fifth place.
```
### Input Table(s)
`users`: one row per user. Primary key `user_id`.
| column | type | meaning |
|---|---|---|
| user_id | INT | User identifier |
| username | VARCHAR(100) | Display name, NOT NULL, not guaranteed unique |
`repositories`: one row per repository. Primary key `repository_id`.
| column | type | meaning |
|---|---|---|
| repository_id | INT | Repository identifier |
| repo_name | VARCHAR(200) | Repository name, NOT NULL |
`commits`: one row per commit. Primary key `commit_id`. `user_id` references `users.user_id` and `repository_id` references `repositories.repository_id`.
| column | type | meaning |
|---|---|---|
| commit_id | INT | Commit identifier |
| user_id | INT | Author of the commit |
| repository_id | INT | Repository the commit belongs to |
| commit_date | DATE | Day of the commit, NOT NULL |
| message | VARCHAR(500) | Commit message, may be NULL |
### Output Contract
| column | meaning |
|---|---|
| user_id | User identifier |
| username | The user's name from `users` |
| commit_count | Number of the user's commits in repository 1 inside the date window |
Order by `commit_count` descending, ties by `user_id` ascending, and limit the output to 5 rows. `user_id` is unique, so the order and the cut at 5 rows are deterministic.
### Example
`users`
| user_id | username |
|---|---|
| 1 | alice |
| 2 | bob |
| 3 | carol |
| 4 | dave |
| 5 | erin |
| 6 | frank |
| 7 | grace |
`repositories`
| repository_id | repo_name |
|---|---|
| 1 | core-api |
| 2 | web-client |
`commits`
| commit_id | user_id | repository_id | commit_date | message |
|---|---|---|---|---|
| 1001 | 3 | 1 | 2024-01-02 | Add rate limiter |
| 1002 | 3 | 1 | 2024-01-10 | Fix retry bug |
| 1003 | 3 | 1 | 2024-01-30 | Bump version |
| 1004 | 1 | 1 | 2024-01-05 | Refactor auth |
| 1005 | 1 | 1 | 2024-01-20 | NULL |
| 1006 | 4 | 1 | 2024-01-01 | Initial schema |
| 1007 | 4 | 1 | 2024-01-15 | Add index |
| 1008 | 2 | 1 | 2024-01-12 | Update docs |
| 1009 | 2 | 1 | 2024-01-31 | Release notes |
| 1010 | 2 | 2 | 2024-01-08 | Fix CSS |
| 1011 | 6 | 1 | 2024-01-25 | Add tests |
| 1012 | 7 | 1 | 2024-01-29 | Lint fixes |
| 1013 | 5 | 1 | 2023-12-31 | Year-end cleanup |
| 1014 | 5 | 2 | 2024-01-03 | Add dark mode |
| 1015 | 1 | 2 | 2024-01-04 | Fix typo |
Expected result:
| user_id | username | commit_count |
|---|---|---|
| 3 | carol | 3 |
| 1 | alice | 2 |
| 4 | dave | 2 |
| 2 | bob | 1 |
| 6 | frank | 1 |
Bob's commit on 2024-01-31 falls outside the window and his commit in repository 2 does not count, so he has 1. Bob, frank and grace tie at 1, and the 5-row limit keeps the two smallest ids (2 and 6). Erin has no commit in repository 1 inside the window, so she does not appear.
### Constraints and Clarifications
- `users` has at most 10,000 rows and `commits` at most 100,000 rows.
- `commit_date` is a valid `DATE` and is never NULL.
- The window is half-open: it includes 2024-01-01 and excludes 2024-01-31, so it covers January 1 through January 30.
- Only commits with `repository_id = 1` count. A user's commits in other repositories are ignored.
- Each row in `commits` is one commit. Several commits by the same user on the same day each count.
- Every `user_id` in `commits` matches a row in `users`. Usernames can repeat across users, so a user is identified by `user_id`.
- Users with no qualifying commit do not appear. If fewer than 5 users qualify, return all of them.
- `message` may be NULL and has no effect on the count.
Overview: Write a SQL query that finds the five users with the most commits to one repository between January 1 and January 30, 2024, breaking ties by user id. It tests date filtering with a half-open range, grouping and counting per user, and deterministic top-N ordering.
Find the top 5 users by number of commits in repository_id = 1 with commit_date >= '2024-01-01' and commit_date < '2024-01-31'. Return user_id, username and commit_count.
Input Table(s)
users: one row per user. Primary key user_id.
column
type
meaning
user_id
INT
User identifier
username
VARCHAR(100)
Display name, NOT NULL, not guaranteed unique
repositories: one row per repository. Primary key repository_id.
column
type
meaning
repository_id
INT
Repository identifier
repo_name
VARCHAR(200)
Repository name, NOT NULL
commits: one row per commit. Primary key commit_id. user_id references users.user_id and repository_id references repositories.repository_id.
column
type
meaning
commit_id
INT
Commit identifier
user_id
INT
Author of the commit
repository_id
INT
Repository the commit belongs to
commit_date
DATE
Day of the commit, NOT NULL
message
VARCHAR(500)
Commit message, may be NULL
Output Contract
column
meaning
user_id
User identifier
username
The user's name from users
commit_count
Number of the user's commits in repository 1 inside the date window
Order by commit_count descending, ties by user_id ascending, and limit the output to 5 rows. user_id is unique, so the order and the cut at 5 rows are deterministic.
Example
users
user_id
username
1
alice
2
bob
3
carol
4
dave
5
erin
6
frank
7
grace
repositories
repository_id
repo_name
1
core-api
2
web-client
commits
commit_id
user_id
repository_id
commit_date
message
1001
3
1
2024-01-02
Add rate limiter
1002
3
1
2024-01-10
Fix retry bug
1003
3
1
2024-01-30
Bump version
1004
1
1
2024-01-05
Refactor auth
1005
1
1
2024-01-20
NULL
1006
4
1
2024-01-01
Initial schema
1007
4
1
2024-01-15
Add index
1008
2
1
2024-01-12
Update docs
1009
2
1
2024-01-31
Release notes
1010
2
2
2024-01-08
Fix CSS
1011
6
1
2024-01-25
Add tests
1012
7
1
2024-01-29
Lint fixes
1013
5
1
2023-12-31
Year-end cleanup
1014
5
2
2024-01-03
Add dark mode
1015
1
2
2024-01-04
Fix typo
Expected result:
user_id
username
commit_count
3
carol
3
1
alice
2
4
dave
2
2
bob
1
6
frank
1
Bob's commit on 2024-01-31 falls outside the window and his commit in repository 2 does not count, so he has 1. Bob, frank and grace tie at 1, and the 5-row limit keeps the two smallest ids (2 and 6). Erin has no commit in repository 1 inside the window, so she does not appear.
Constraints and Clarifications
users
has at most 10,000 rows and
commits
at most 100,000 rows.
commit_date
is a valid
DATE
and is never NULL.
The window is half-open: it includes 2024-01-01 and excludes 2024-01-31, so it covers January 1 through January 30.
Only commits with
repository_id = 1
count. A user's commits in other repositories are ignored.
Each row in
commits
is one commit. Several commits by the same user on the same day each count.
Every
user_id
in
commits
matches a row in
users
. Usernames can repeat across users, so a user is identified by
user_id
.
Users with no qualifying commit do not appear. If fewer than 5 users qualify, return all of them.
message
may be NULL and has no effect on the count.