Top 5 Projects by Budget per Distinct Assigned Employee
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
A company tracks its projects and which employees are assigned to each one. Return the top 5 projects by `budget_per_employee`, defined as the project's `budget` divided by the number of distinct employees assigned to it, rounded to 2 decimals. Return `project_id`, `project_name` and `budget_per_employee`, ordered by `budget_per_employee` descending, then `project_id` ascending.
`employee_projects` may repeat an `(employee_id, project_id)` pair; count each employee once per project. Exclude projects with no assigned employees.
```hint Check the denominator
Look at what happens to the divisor when one person is listed twice on the same project, and at what happens to a project that never appears in `employee_projects`.
```
### Input Table(s)
`projects`
| Column | Type | Meaning |
|---|---|---|
| `project_id` | INT | Project identifier. Primary key. |
| `project_name` | VARCHAR(100) | Project's name. Not null. |
| `budget` | DECIMAL(12,2) | Total project budget. Not null. |
`employee_projects`
| Column | Type | Meaning |
|---|---|---|
| `assignment_id` | INT | Assignment identifier. Primary key. |
| `employee_id` | INT | Assigned employee. Not null. |
| `project_id` | INT | Project the employee is assigned to. Not null. |
Keys: `projects.project_id` and `employee_projects.assignment_id` are primary keys. `employee_projects.project_id` refers to `projects.project_id`. The pair `(employee_id, project_id)` is not unique.
### Output Contract
- Columns, in this order: `project_id`, `project_name`, `budget_per_employee`.
- `budget_per_employee` is `budget / (number of distinct employee_id values on the project)`, rounded to 2 decimal places.
- Only projects with at least one assignment are ranked.
- Sort by `budget_per_employee` (the rounded value) descending, then `project_id` ascending, and return the first 5 rows. Because `project_id` is unique, a tie at fifth place is settled by `project_id` and the result has exactly 5 rows, or fewer when fewer than 5 projects have assignments.
### Example
`projects`
| project_id | project_name | budget |
|---|---|---|
| 1 | Atlas | 300000.00 |
| 2 | Beacon | 200000.00 |
| 3 | Comet | 40000.00 |
| 4 | Delta | 500000.00 |
| 5 | Ember | 80000.00 |
| 6 | Falcon | 100000.00 |
| 7 | Granite | 40000.00 |
`employee_projects`
| assignment_id | employee_id | project_id |
|---|---|---|
| 1 | 10 | 1 |
| 2 | 11 | 1 |
| 3 | 12 | 1 |
| 4 | 10 | 1 |
| 5 | 10 | 2 |
| 6 | 13 | 2 |
| 7 | 19 | 2 |
| 8 | 14 | 3 |
| 9 | 11 | 5 |
| 10 | 15 | 5 |
| 11 | 12 | 6 |
| 12 | 16 | 6 |
| 13 | 16 | 6 |
| 14 | 17 | 7 |
Expected result:
| project_id | project_name | budget_per_employee |
|---|---|---|
| 1 | Atlas | 100000.00 |
| 2 | Beacon | 66666.67 |
| 6 | Falcon | 50000.00 |
| 3 | Comet | 40000.00 |
| 5 | Ember | 40000.00 |
Atlas lists employee 10 twice, so it has 3 distinct employees and 300000.00 / 3 = 100000.00. Beacon is 200000.00 / 3 = 66666.666..., rounded to 66666.67. Falcon lists employee 16 twice, so it divides by 2. Delta has the largest budget but no assignments, so it is excluded. Comet, Ember and Granite all come to 40000.00; `project_id` orders them 3, 5, 7, and Granite falls outside the top 5.
### Constraints and Clarifications
- There are 1 to 10,000 projects.
- `budget` is at least 1 and below ${10^{10}}$, the range `DECIMAL(12,2)` can hold.
- `employee_id` and `project_id` in `employee_projects` are never NULL.
- Repeated `(employee_id, project_id)` rows count as one employee for that project. The same employee can still be counted on several different projects.
- Rounding is standard half-up rounding to 2 decimal places, as PostgreSQL's `ROUND(numeric, 2)` does.
- A project that appears in `projects` but has no row in `employee_projects` is not part of the result.
Overview: Write a SQL query that ranks projects by budget per distinct assigned employee and returns the top five, rounded to two decimals. It tests counting distinct values in a join table with duplicate rows, excluding projects with no assignments, and deterministic ordering with a tiebreaker under LIMIT.