Find the top 5 users by write actions (INSERT, UPDATE, DELETE) in a 24-hour window
Company: Okta
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 5 users by number of write actions (`'INSERT'`, `'UPDATE'`, `'DELETE'`) in the last 24 hours: the window from 2024-01-14 00:00:00 to 2024-01-15 00:00:00. Users with no write actions in the window are excluded, so the result may be empty.
```hint Fixed window
The "last 24 hours" here are two fixed timestamps, not a span measured back from the moment the query runs. Work out how long the window would be if both endpoints were included.
```
### Input Table(s)
`users`
| Column | Type | Meaning |
|---|---|---|
| user_id | INT | User identifier |
| username | VARCHAR(100) | 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, never NULL |
| action_type | VARCHAR(20) | One of `INSERT`, `UPDATE`, `DELETE`, `SELECT`, `READ`; 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`, `write_action_count` (the integer number of write actions in the window).
- One row per user with at least one write action in the window, at most 5 rows.
- Rows sorted by `write_action_count` descending, then `user_id` ascending.
- Counts are integers, so no rounding applies.
### Example
`users`
| user_id | username |
|---|---|
| 1 | ana |
| 2 | ben |
| 3 | chen |
| 4 | dara |
| 5 | eli |
| 6 | fay |
| 7 | gus |
`actions`
| action_id | user_id | action_type | action_time |
|---|---|---|---|
| 201 | 1 | INSERT | 2024-01-14 00:00:00 |
| 202 | 1 | UPDATE | 2024-01-14 09:30:00 |
| 203 | 1 | SELECT | 2024-01-14 09:31:00 |
| 204 | 2 | INSERT | 2024-01-14 10:00:00 |
| 205 | 2 | DELETE | 2024-01-14 12:15:00 |
| 206 | 2 | UPDATE | 2024-01-14 23:59:59 |
| 207 | 3 | UPDATE | 2024-01-13 23:59:59 |
| 208 | 3 | DELETE | 2024-01-14 15:45:00 |
| 209 | 3 | INSERT | 2024-01-15 00:00:00 |
| 210 | 4 | INSERT | 2024-01-14 08:05:00 |
| 211 | 4 | READ | 2024-01-14 08:06:00 |
| 212 | 5 | UPDATE | 2024-01-14 17:20:00 |
| 213 | 5 | UPDATE | 2024-01-14 17:25:00 |
| 214 | 6 | INSERT | 2024-01-14 21:00:00 |
| 215 | 7 | READ | 2024-01-14 11:00:00 |
Expected result:
| user_id | username | write_action_count |
|---|---|---|
| 2 | ben | 3 |
| 1 | ana | 2 |
| 5 | eli | 2 |
| 3 | chen | 1 |
| 4 | dara | 1 |
ben has three writes inside the window. ana's insert at exactly 2024-01-14 00:00:00 counts and her `SELECT` does not, so she has 2, like eli. chen has 1 because action 207 comes one second before the window and action 209 lands exactly on its excluded end. chen, dara and fay tie at 1 for the last two places, and the `user_id` tiebreak keeps chen and dara. gus has only a `READ` and does not appear.
### Constraints and Clarifications
- The window is half-open and exactly 24 hours long. An action at exactly `2024-01-14 00:00:00` counts; an action at exactly `2024-01-15 00:00:00` does not.
- The window is fixed. It does not depend on the current date or time.
- Only `INSERT`, `UPDATE` and `DELETE` are write actions, stored in uppercase as listed. `SELECT` and `READ` rows are ignored.
- Users with no write action in the window are not returned, so the result can have fewer than 5 rows or none at all.
- Users who tie on `write_action_count` are ordered by `user_id` ascending, and that rule also decides who takes the fifth place.
- Every `actions.user_id` is non-NULL and references a valid user. Each row of `actions` is one action (`action_id` is unique), so no deduplication is needed.
- `users` has at most 10,000 rows and `actions` at most 100,000 rows.
Overview: Write a SQL query that counts each user's INSERT, UPDATE and DELETE actions inside a fixed 24-hour window and returns the five most active writers. It tests join and group-by aggregation, half-open timestamp filtering, and a deterministic tiebreak when users tie at the cutoff.