Find the top 3 users by comment and status-change actions in a one-week window
Company: Asana
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
The `users` table has one row per user, and the `actions` table logs each action a user takes. Find the top 3 users by number of `comment` or `status_change` actions with `action_time` in `[2024-01-08 00:00:00, 2024-01-15 00:00:00)`. Return `user_id`, `username`, `action_count`, ordered by `action_count` descending, then `user_id` ascending. If fewer than 3 users qualify, return all qualifying users.
```hint Window boundaries
Decide which side of the window an action stamped exactly 2024-01-08 00:00:00 falls on, and then one stamped exactly 2024-01-15 00:00:00.
```
### Input Table(s)
`users`
| Column | Type | Meaning |
|---|---|---|
| user_id | INT | User identifier |
| username | VARCHAR(50) | User's name, never NULL |
Primary key: `user_id`.
`actions`
| Column | Type | Meaning |
|---|---|---|
| action_id | INT | Action identifier |
| user_id | INT | User who took the action |
| action_type | VARCHAR(30) | Kind of action, such as `comment`, `status_change`, `login` or `logout`; never NULL |
| action_time | TIMESTAMP | When the action happened (DATETIME in MySQL); never NULL |
Primary key: `action_id`. Foreign key: `actions.user_id` references `users.user_id`.
### Output Contract
- Columns, in this order: `user_id`, `username`, `action_count` (the integer number of qualifying actions).
- One row per qualifying user, at most 3 rows.
- Rows sorted by `action_count` descending, then `user_id` ascending.
- Counts are integers, so no rounding applies.
### Example
`users`
| user_id | username |
|---|---|
| 1 | alice |
| 2 | bob |
| 3 | carol |
| 4 | dave |
| 5 | erin |
`actions`
| action_id | user_id | action_type | action_time |
|---|---|---|---|
| 101 | 1 | comment | 2024-01-08 09:15:00 |
| 102 | 1 | status_change | 2024-01-10 14:00:00 |
| 103 | 1 | login | 2024-01-10 13:55:00 |
| 104 | 2 | comment | 2024-01-07 23:59:59 |
| 105 | 2 | comment | 2024-01-09 11:00:00 |
| 106 | 2 | status_change | 2024-01-14 23:59:59 |
| 107 | 3 | comment | 2024-01-15 00:00:00 |
| 108 | 3 | comment | 2024-01-12 08:30:00 |
| 109 | 3 | status_change | 2024-01-08 00:00:00 |
| 110 | 4 | status_change | 2024-01-11 16:20:00 |
| 111 | 4 | comment | 2024-01-13 10:05:00 |
| 112 | 4 | comment | 2024-01-13 10:40:00 |
| 113 | 5 | logout | 2024-01-09 18:00:00 |
Expected result:
| user_id | username | action_count |
|---|---|---|
| 4 | dave | 3 |
| 1 | alice | 2 |
| 2 | bob | 2 |
dave has three qualifying actions. alice, bob and carol have two each: action 103 is a login, action 104 is one second before the window opens, action 107 lands exactly on the excluded end, and action 109 lands exactly on the included start. erin has only a logout and does not qualify. The three-way tie at 2 is broken by `user_id`, so alice and bob fill the last two places and carol is cut.
### Constraints and Clarifications
- The window is half-open. An action at exactly `2024-01-08 00:00:00` counts; an action at exactly `2024-01-15 00:00:00` does not.
- Only the `action_type` values `comment` and `status_change` count. Every other type (`login`, `logout` and similar) is ignored.
- A user qualifies only with at least one qualifying action, so a user with a count of zero is never returned.
- Users who tie on `action_count` are ordered by `user_id` ascending, and that rule also decides who takes the third place.
- Each row of `actions` is one action (`action_id` is unique), so no deduplication is needed.
- An action whose `user_id` is NULL belongs to no user and is not counted.
- `action_time` carries no time zone. Compare the stored values as they are.
Overview: Write a SQL query that joins users to their actions, counts comment and status-change actions inside a half-open one-week window, and returns the three most active users. It tests timestamp range filtering, grouping and counting, and deterministic tie-breaking with ORDER BY and LIMIT.