Rank users by total session minutes in January 2024 using dense ranking
Company: X (Twitter)
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Rank users by total session duration for sessions with `session_start` on or after `'2024-01-01'` and before `'2024-02-01'`. Return `user_id`, `username`, `total_duration_minutes` and `activity_rank`.
```hint Minutes from an interval
Check what your expression returns for a session that lasts 1 hour 30 minutes. It should count as 90.
```
### Input Table(s)
`users`
| Column | Type | Meaning |
|---|---|---|
| user_id | INT | User identifier. Primary key. |
| username | VARCHAR(100) | Display name. NOT NULL. |
`sessions`
| Column | Type | Meaning |
|---|---|---|
| session_id | INT | Session identifier. Primary key. |
| user_id | INT | The user. Foreign key to `users.user_id`. |
| session_start | TIMESTAMP | When the session began (DATETIME in MySQL). NOT NULL. |
| session_end | TIMESTAMP | When the session ended. NOT NULL, always after `session_start`. |
### Output Contract
- Columns, in this order: `user_id`, `username`, `total_duration_minutes`, `activity_rank`.
- A session's duration is the number of whole minutes from `session_start` to `session_end`, with any partial minute dropped. `total_duration_minutes` is the sum of those values over the user's sessions in the range, as an integer.
- `activity_rank` is `DENSE_RANK()` on `total_duration_minutes` descending: ties share a rank and the next rank follows with no gap.
- Order by `activity_rank` ascending, then `user_id` ascending. `user_id` is unique in the result, so the order is fully determined.
### Example
`users`
| user_id | username |
|---|---|
| 1 | ava |
| 2 | ben |
| 3 | cy |
| 4 | dee |
| 5 | eli |
`sessions`
| session_id | user_id | session_start | session_end |
|---|---|---|---|
| 1 | 1 | 2024-01-02 09:00:00 | 2024-01-02 10:30:00 |
| 2 | 1 | 2024-01-31 23:50:00 | 2024-02-01 00:20:00 |
| 3 | 2 | 2024-01-10 08:00:00 | 2024-01-10 10:00:00 |
| 4 | 3 | 2024-01-15 12:00:00 | 2024-01-15 12:45:40 |
| 5 | 3 | 2023-12-31 23:00:00 | 2024-01-01 01:00:00 |
| 6 | 4 | 2024-02-01 00:00:00 | 2024-02-01 03:00:00 |
| 7 | 5 | 2024-01-20 18:00:00 | 2024-01-20 18:00:30 |
Expected result:
| user_id | username | total_duration_minutes | activity_rank |
|---|---|---|---|
| 1 | ava | 120 | 1 |
| 2 | ben | 120 | 1 |
| 3 | cy | 45 | 2 |
| 5 | eli | 0 | 3 |
Ava has 90 + 30 = 120 minutes. Her second session counts in full because it starts on January 31, even though it ends in February. Ben's single session is also 120 minutes, so the two share rank 1. Cy's January session lasts 45 minutes 40 seconds, which counts as 45, and his December session is outside the range. Dee's only session starts on February 1 and is excluded. Eli's 30-second session counts as 0 minutes, but he still has a session in the range and appears with rank 3.
### Constraints and Clarifications
- Only `session_start` decides whether a session is in the range. A session that starts in January counts in full wherever it ends.
- Include only users with at least one session in the range. A user whose sessions in the range are all shorter than a minute appears with a total of 0.
- Every session row counts, including overlapping sessions of the same user and rows that differ only in `session_id`.
- A session row with a NULL `user_id` belongs to no user and is not reported.
- `users` has at most 10,000 rows and `sessions` at most 100,000 rows.
- `session_end` is always after `session_start`.
Overview: Write a SQL query that totals each user's session time in whole minutes for sessions starting in January 2024 and ranks users with DENSE_RANK. It tests timestamp interval arithmetic, half-open date filters, and window-function ranking with ties.