Top 5 Engaged Users by Interaction Count in a Date Window
Company: Zoom
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
A meetings app stores its users in one table and logs every in-meeting interaction (messages, reactions, screen shares, poll votes) in another. Find the top 5 users by interaction count where `interaction_date >= '2024-01-01'` and `interaction_date < '2024-01-31'`. Return `user_id`, `username` and `total_interactions`, ordered by `total_interactions` descending, then `user_id` ascending.
```hint Filter, then count, then cut
Restrict rows to the window before you aggregate, and check what decides the fifth place when several users have the same count.
```
### Input Table(s)
`users` (primary key `user_id`)
| column | type | meaning |
|---|---|---|
| user_id | INT | Unique user identifier |
| username | VARCHAR | User's display name |
`interactions` (primary key `interaction_id`; `user_id` references `users.user_id`)
| column | type | meaning |
|---|---|---|
| interaction_id | INT | Unique interaction identifier |
| user_id | INT | User who performed the interaction |
| interaction_type | VARCHAR | Kind of interaction, for example `'message'`, `'reaction'`, `'screen_share'`, `'poll_vote'` |
| interaction_date | DATE | Day the interaction happened |
### Output Contract
- Columns, in this order: `user_id`, `username`, `total_interactions` (integer count of the user's interactions inside the window).
- Order by `total_interactions` descending, then `user_id` ascending.
- Return at most 5 rows. Only users with at least one interaction in the window can appear, so fewer than 5 rows come back when fewer than 5 users qualify.
- No rounding is involved.
### Example
`users`
| user_id | username |
|---|---|
| 1 | alice |
| 2 | bob |
| 3 | carol |
| 4 | dave |
| 5 | erin |
| 6 | frank |
| 7 | grace |
`interactions`
| interaction_id | user_id | interaction_type | interaction_date |
|---|---|---|---|
| 101 | 1 | message | 2024-01-02 |
| 102 | 1 | reaction | 2024-01-05 |
| 103 | 1 | poll_vote | 2024-01-30 |
| 104 | 2 | message | 2024-01-01 |
| 105 | 2 | screen_share | 2024-01-15 |
| 106 | 3 | reaction | 2024-01-10 |
| 107 | 3 | message | 2024-01-12 |
| 108 | 4 | message | 2024-01-20 |
| 109 | 5 | poll_vote | 2024-01-21 |
| 110 | 6 | reaction | 2024-01-22 |
| 111 | 7 | message | 2024-01-31 |
| 112 | 7 | message | 2024-01-31 |
| 113 | 7 | reaction | 2024-01-31 |
| 114 | 7 | screen_share | 2023-12-31 |
| 115 | 2 | message | 2024-02-01 |
Expected result
| user_id | username | total_interactions |
|---|---|---|
| 1 | alice | 3 |
| 2 | bob | 2 |
| 3 | carol | 2 |
| 4 | dave | 1 |
| 5 | erin | 1 |
Rows 101 to 110 fall inside the window. grace's interactions are all on 2024-01-31 or 2023-12-31, so she has none in range, and bob's row on 2024-02-01 is also excluded. dave, erin and frank each have 1 interaction; the `user_id` tie-break puts dave and erin in the last two places and leaves frank out.
### Constraints and Clarifications
- `users` has at most 10,000 rows and `interactions` at most 100,000 rows.
- Each `interaction_id` is unique, so each row is one interaction.
- The window covers 2024-01-01 through 2024-01-30. Interactions dated 2024-01-31 are outside it.
- Every interaction type counts; there is no filter on `interaction_type`.
- When users tie on `total_interactions`, the lower `user_id` ranks first, and that rule also decides who gets the last of the 5 places.
- Interactions with a NULL `interaction_date`, a NULL `user_id`, or a `user_id` that has no row in `users` are not counted.
Overview: Write a SQL query that counts each user's interactions inside a January 2024 date window and returns the five most active users with their usernames. It tests joins, half-open date filters, grouping, and a tie-break that keeps the row limit deterministic.