Find the top 10 users by total storage uploaded in a 30-day window
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Find the top 10 users by total uploaded storage for files with `uploaded_at` in the half-open window `['2024-01-01', '2024-01-31')`. Return `user_id`, `username` and `total_storage_mb` (rounded to 2 decimals), ordered by `total_storage_mb` descending, then `user_id` ascending.
### Input Table(s)
`users`, primary key `user_id`:
| Column | Type | Meaning |
|---|---|---|
| user_id | INT | Unique user id |
| username | VARCHAR(100) | Display name, NOT NULL |
| email | VARCHAR(255) | Email address, NOT NULL |
`files`, primary key `file_id`, with `user_id` a foreign key to `users.user_id`:
| Column | Type | Meaning |
|---|---|---|
| file_id | INT | Unique file id |
| user_id | INT | Owner of the file |
| file_name | VARCHAR(255) | Name of the file, NOT NULL |
| file_size_mb | DECIMAL(10,2) | File size in megabytes, NOT NULL, always positive |
| uploaded_at | DATETIME (TIMESTAMP in PostgreSQL) | Date and time of the upload, NOT NULL |
### Output Contract
- Columns, in this order: `user_id`, `username`, `total_storage_mb`.
- `total_storage_mb` is the sum of `file_size_mb` over the user's files in the window, rounded to 2 decimal places.
- At most 10 rows, one per user.
- Sort by `total_storage_mb` descending, then by `user_id` ascending. The same order decides which users make the top 10.
### Example
`users`:
| user_id | username | email |
|---|---|---|
| 1 | ana | ana@example.com |
| 2 | ben | ben@example.com |
| 3 | cy | cy@example.com |
| 4 | dee | dee@example.com |
| 5 | eli | eli@example.com |
`files`:
| file_id | user_id | file_name | file_size_mb | uploaded_at |
|---|---|---|---|---|
| 201 | 1 | report.pdf | 120.40 | 2024-01-01 00:00:00 |
| 202 | 1 | deck.pptx | 80.10 | 2024-01-30 23:59:59 |
| 203 | 2 | movie.mkv | 200.50 | 2024-01-15 09:30:00 |
| 204 | 2 | backup.tar | 900.00 | 2024-01-31 00:00:00 |
| 205 | 3 | photos.zip | 350.00 | 2024-01-20 14:05:00 |
| 206 | 4 | notes.txt | 0.75 | 2023-12-31 23:59:59 |
| 207 | 5 | design.psd | 150.25 | 2024-01-05 08:00:00 |
| 208 | 5 | assets.zip | 50.25 | 2024-01-25 17:45:00 |
Expected result:
| user_id | username | total_storage_mb |
|---|---|---|
| 3 | cy | 350.00 |
| 1 | ana | 200.50 |
| 2 | ben | 200.50 |
| 5 | eli | 200.50 |
Cy leads with 350.00. Ana's files at both edges of the window count, giving 120.40 + 80.10 = 200.50. Ben's backup at exactly 2024-01-31 00:00:00 is outside the half-open window, so he has 200.50 from one file, and eli has 150.25 + 50.25 = 200.50. The three-way tie is ordered by `user_id`. Dee's only file is from December, so she has no row. Only four users qualify, so all four fit under the limit of 10.
### Constraints and Clarifications
- `users` has at most 10,000 rows and `files` at most 100,000 rows.
- The window includes 2024-01-01 00:00:00 and every time before 2024-01-31 00:00:00, so the last day counted is January 30.
- Users with no files in the window are not returned. If fewer than 10 users uploaded in the window, return fewer than 10 rows.
- Return at most 10 rows even when several users tie for 10th place: the `user_id` tie-break decides who makes the cut.
- `file_size_mb` is always positive, and every `user_id` in `files` references a valid user.
- Each row of `files` is a separate file, so files that share a name all count toward the total.
- Usernames are not guaranteed to be unique, so identify users by `user_id`.
```hint Window edges
Check whether files uploaded at 2024-01-30 23:59:59 and at 2024-01-31 00:00:00 land inside your filter.
```
Overview: Write a SQL query that ranks users by the total size of files they uploaded in a half-open 30-day window and returns the top 10, with totals rounded to two decimals. It tests joins, timestamp range filters, aggregation, and tie-breaking at a LIMIT cutoff.