Average File Size and File Count per User per Billing Period
Company: Box
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
A file storage service records its users, its billing periods and every file a user uploads. For every user and every billing period, compute the average file size in MB and the number of files the user uploaded in that period. Return `user_id`, `username`, `period_id`, `period_name`, `avg_storage_mb` and `total_files`.
```hint Zero-file rows
Make sure the row for a user with no files in a period exists before any file-level condition is applied, then check what `COUNT` and `AVG` return for that row.
```
### Input Table(s)
`users`
| Column | Type | Meaning |
|---|---|---|
| user_id | INT | Unique identifier of the user |
| username | VARCHAR | The user's name |
Primary key: `user_id`.
`billing_periods`
| Column | Type | Meaning |
|---|---|---|
| period_id | INT | Unique identifier of the billing period |
| period_name | VARCHAR | Display name of the period |
| start_date | DATE | First day of the period (inclusive) |
| end_date | DATE | Last day of the period (inclusive) |
Primary key: `period_id`.
`files`
| Column | Type | Meaning |
|---|---|---|
| file_id | INT | Unique identifier of the uploaded file |
| user_id | INT | User who uploaded the file; references `users.user_id` |
| file_size_mb | DECIMAL(10,2) | Size of the file in MB, always positive |
| upload_date | DATE | Day the file was uploaded |
Primary key: `file_id`.
### Output Contract
| Column | Type | Meaning |
|---|---|---|
| user_id | INT | The user |
| username | VARCHAR | The user's name |
| period_id | INT | The billing period |
| period_name | VARCHAR | The period's name |
| avg_storage_mb | DECIMAL | Average `file_size_mb` of the user's files uploaded in the period, rounded to 2 decimal places; `0.00` when there are none |
| total_files | INTEGER | Number of files the user uploaded in the period; `0` when there are none |
- One row for every combination of a user and a billing period, so the result has (number of users) × (number of periods) rows.
- Rows are ordered by `user_id` ascending, then `period_id` ascending. Each pair appears once, so this order is unique.
### Example
`users`
| user_id | username |
|---|---|
| 1 | alice |
| 2 | bob |
| 3 | chen |
`billing_periods`
| period_id | period_name | start_date | end_date |
|---|---|---|---|
| 1 | 2024-Q1 | 2024-01-01 | 2024-03-31 |
| 2 | 2024-Q2 | 2024-04-01 | 2024-06-30 |
`files`
| file_id | user_id | file_size_mb | upload_date |
|---|---|---|---|
| 101 | 1 | 12.50 | 2024-01-15 |
| 102 | 1 | 7.25 | 2024-03-31 |
| 103 | 1 | 3.10 | 2024-04-01 |
| 104 | 2 | 100.00 | 2024-05-20 |
| 105 | 2 | 50.00 | 2024-06-02 |
| 106 | 2 | 20.00 | 2024-06-30 |
| 107 | 3 | 8.00 | 2024-07-04 |
Expected output:
| user_id | username | period_id | period_name | avg_storage_mb | total_files |
|---|---|---|---|---|---|
| 1 | alice | 1 | 2024-Q1 | 9.88 | 2 |
| 1 | alice | 2 | 2024-Q2 | 3.10 | 1 |
| 2 | bob | 1 | 2024-Q1 | 0.00 | 0 |
| 2 | bob | 2 | 2024-Q2 | 56.67 | 3 |
| 3 | chen | 1 | 2024-Q1 | 0.00 | 0 |
| 3 | chen | 2 | 2024-Q2 | 0.00 | 0 |
File 102 was uploaded on the last day of Q1 and file 103 on the first day of Q2, so both period bounds count. Alice's Q1 average is (12.50 + 7.25) / 2 = 9.875, which rounds to 9.88. Bob's Q2 average is (100.00 + 50.00 + 20.00) / 3 = 56.666..., which rounds to 56.67. File 107 falls outside every period, so chen still gets two zero rows.
### Constraints and Clarifications
- `users` has 1 to 10,000 rows, `billing_periods` has 1 to 100 rows, and `files` has 1 to 100,000 rows.
- `file_size_mb` is a positive decimal. No column is NULL.
- A billing period includes both its `start_date` and its `end_date`.
- Each `upload_date` falls within at most one billing period. A file uploaded outside every period is not counted anywhere.
- Every `user_id` in `files` exists in `users`.
- Users with no files in a period still appear, with `avg_storage_mb` = `0.00` and `total_files` = `0`.
Overview: Write a SQL query that reports, for every user and every billing period, the average uploaded file size in MB and the number of files, keeping user and period pairs with no uploads as zeros. It tests building a CROSS JOIN grid, a LEFT JOIN with a date range condition, and handling NULL aggregates.