Rank users by total gigabytes uploaded in a 30-day window
Company: Box
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
For uploads with `upload_date` in `['2024-01-01', '2024-01-31')` (start inclusive, end exclusive), return each user's `user_id`, `username`, and total uploaded data in GB as `total_gb` (rounded to 2 decimals). Include only users with at least one upload in that period. Order by `total_gb` descending, then `user_id` ascending.
### Input Table(s)
`users`, primary key `user_id`:
| Column | Type | Meaning |
|---|---|---|
| user_id | INT | Unique user id |
| username | VARCHAR | Display name |
`uploads`, primary key `upload_id`, with `user_id` referring to `users.user_id`:
| Column | Type | Meaning |
|---|---|---|
| upload_id | INT | Unique upload id |
| user_id | INT | User who made the upload |
| file_size_mb | DECIMAL | Size of the upload in megabytes, always positive |
| upload_date | DATE | Calendar date of the upload |
### Output Contract
- Columns, in this order: `user_id`, `username`, `total_gb`.
- `total_gb` is the user's summed `file_size_mb` in the window divided by 1024 (1 GB = 1024 MB), rounded to 2 decimal places.
- One row per user with at least one upload in the window.
- Sort by the rounded `total_gb` descending, then by `user_id` ascending.
### Example
`users`:
| user_id | username |
|---|---|
| 1 | ana |
| 2 | ben |
| 3 | cy |
| 4 | dee |
`uploads`:
| upload_id | user_id | file_size_mb | upload_date |
|---|---|---|---|
| 301 | 1 | 1024.00 | 2024-01-01 |
| 302 | 2 | 1000.00 | 2024-01-10 |
| 303 | 2 | 27.00 | 2024-01-30 |
| 304 | 3 | 2048.00 | 2024-01-31 |
| 305 | 3 | 512.00 | 2024-01-15 |
| 306 | 4 | 300.00 | 2023-12-31 |
Expected result:
| user_id | username | total_gb |
|---|---|---|
| 1 | ana | 1.00 |
| 2 | ben | 1.00 |
| 3 | cy | 0.50 |
Ana has 1024.00 MB, exactly 1.00 GB. Ben has 1000.00 + 27.00 = 1027.00 MB, about 1.0029 GB, which also rounds to 1.00, so the tie on `total_gb` is broken by `user_id` and ana comes first. Cy's 2024-01-31 upload is outside the window, leaving 512.00 MB, or 0.50 GB. Dee's only upload is from December, so dee has no row.
### Constraints and Clarifications
- `users` has at most 10,000 rows and `uploads` at most 100,000 rows.
- The window covers 2024-01-01 through 2024-01-30. Uploads dated 2024-01-31 are excluded.
- Convert the user's total, then round. Rounding each upload before summing gives different results.
- Ordering uses the rounded `total_gb`, so users whose totals round to the same value are ordered by `user_id`, as in the example.
- Users with no uploads in the window do not appear.
- Each `upload_id` is unique and every upload row counts toward its user's total.
- An upload whose `user_id` has no matching row in `users` is not counted.
```hint Units
Decide at which step the megabyte-to-gigabyte conversion and the rounding happen, and make sure the division keeps its decimal places.
```
Overview: Write a SQL query that totals each user's uploads over a 30-day window, converts megabytes to gigabytes, and ranks users by the rounded total. It tests joins, half-open date filters, aggregation, unit conversion that avoids integer division, and ordering on a rounded value.