List every user folder permission with the files in that folder
Company: Box
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
A file storage service keeps its metadata in four tables: users, folders, the files inside each folder, and the permissions that give a user access to a folder. Return every user-folder permission row along with the files in that folder. Output `user_id`, `username`, `folder_name`, `file_name`, `file_size_kb`, `permission_type`.
If a folder has no files, the file columns are `NULL`. Order by `user_id`, then `folder_id`, then `file_id`, all ascending.
```hint Count the rows first
Take one permission row and work out how many output rows it should produce when its folder holds three files, and when it holds none.
```
### Input Table(s)
Table `Users`
| Column | Type | Meaning |
|---|---|---|
| `user_id` | INT | User id |
| `username` | VARCHAR | User's name |
Primary key: `user_id`.
Table `Folders`
| Column | Type | Meaning |
|---|---|---|
| `folder_id` | INT | Folder id |
| `folder_name` | VARCHAR | Folder name |
| `parent_folder_id` | INT | `folder_id` of the parent folder, `NULL` for a root-level folder |
Primary key: `folder_id`. `parent_folder_id` references `Folders.folder_id`.
Table `Files`
| Column | Type | Meaning |
|---|---|---|
| `file_id` | INT | File id |
| `folder_id` | INT | Folder that holds the file |
| `file_name` | VARCHAR | File name |
| `file_size_kb` | INT | File size in KB |
Primary key: `file_id`. `folder_id` references `Folders.folder_id`.
Table `Permissions`
| Column | Type | Meaning |
|---|---|---|
| `user_id` | INT | User who holds the permission |
| `folder_id` | INT | Folder the permission applies to |
| `permission_type` | VARCHAR | `'read'`, `'write'` or `'admin'` |
Primary key: `(user_id, folder_id)`. `user_id` references `Users.user_id` and `folder_id` references `Folders.folder_id`.
### Output Contract
- Columns, in this order: `user_id`, `username`, `folder_name`, `file_name`, `file_size_kb`, `permission_type`.
- One row for each pair of a permission row and a file in that permission's folder. A permission on a folder with no files gives exactly one row, with `file_name` and `file_size_kb` set to `NULL`.
- Rows ordered by `user_id`, then `folder_id`, then `file_id`, all ascending. `folder_id` and `file_id` are sort keys only and are not output columns. Together the three ids identify each row uniquely.
- Values are returned as stored, with no rounding.
### Example
Input `Users`:
| user_id | username |
|---|---|
| 1 | alice |
| 2 | bob |
| 3 | carol |
Input `Folders`:
| folder_id | folder_name | parent_folder_id |
|---|---|---|
| 10 | Projects | NULL |
| 11 | Reports | 10 |
| 12 | Archive | NULL |
Input `Files`:
| file_id | folder_id | file_name | file_size_kb |
|---|---|---|---|
| 100 | 10 | plan.docx | 120 |
| 101 | 10 | budget.xlsx | 45 |
| 102 | 11 | q3.pdf | 300 |
Input `Permissions`:
| user_id | folder_id | permission_type |
|---|---|---|
| 1 | 10 | admin |
| 1 | 12 | read |
| 2 | 10 | read |
| 2 | 11 | write |
Expected output:
| user_id | username | folder_name | file_name | file_size_kb | permission_type |
|---|---|---|---|---|---|
| 1 | alice | Projects | plan.docx | 120 | admin |
| 1 | alice | Projects | budget.xlsx | 45 | admin |
| 1 | alice | Archive | NULL | NULL | read |
| 2 | bob | Projects | plan.docx | 120 | read |
| 2 | bob | Projects | budget.xlsx | 45 | read |
| 2 | bob | Reports | q3.pdf | 300 | write |
Alice's Archive folder is empty, so it appears once with `NULL` file columns. Her admin permission on Projects does not cover its subfolder Reports, and carol has no permissions, so neither adds rows. Files are ordered by `file_id` (100 before 101) and folders by `folder_id` (10 before 12), not by name.
### Constraints and Clarifications
- Max rows: `Users` 100, `Folders` 200, `Files` 500, `Permissions` 300.
- `permission_type` is one of `'read'`, `'write'`, `'admin'`.
- `file_size_kb` is a positive integer.
- `parent_folder_id` can be `NULL` for root-level folders.
- Permissions are not inherited. A permission covers only the files whose `folder_id` equals the permission's `folder_id`, never files in its subfolders.
- A user has at most one permission row per folder.
- Users with no permission rows and folders nobody has a permission on do not appear in the output.
- Every `user_id` and `folder_id` in `Permissions` exists in `Users` and `Folders`. No column other than `parent_folder_id` is `NULL`.
Overview: A SQL question that joins users, folder permissions, folders and files to list every permission row together with the files in its folder. It tests picking join types that keep empty folders with NULL file columns and ordering by ids for a deterministic result.