Rank Oscar nominees by total wins with tied ranks and gaps
Company: Netflix
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
The `nominees` table lists people nominated for Oscars, and `nominations` has one row per nomination with a flag for whether it won. Rank the nominees who have at least one win by their total number of Oscars won. Use `RANK()` semantics: nominees with the same total share a rank, and the next rank skips ahead by the size of the tie, as in 1, 1, 3.
Return `nominee_id`, `nominee_name`, `total_wins` and `rank`, ordered by `total_wins` descending, then `nominee_id` ascending.
```hint One row per nominee
Get each nominee's win count before you rank. Make sure a nominee whose nominations all lost never reaches the ranking step.
```
### Input Table(s)
Table `nominees`
| Column | Type | Meaning |
|---|---|---|
| nominee_id | INT | Nominee identifier |
| nominee_name | VARCHAR(200) | Nominee name, never NULL, not declared unique |
Primary key: `nominee_id`.
Table `nominations`
| Column | Type | Meaning |
|---|---|---|
| nomination_id | INT | Nomination identifier |
| nominee_id | INT | Nominee who received the nomination |
| year | INT | Ceremony year, never NULL |
| category | VARCHAR(100) | Award category, never NULL |
| won | SMALLINT | 1 if the nomination won, 0 if it did not (default 0) |
Primary key: `nomination_id`. Foreign key: `nominations.nominee_id` references `nominees.nominee_id`.
### Output Contract
- Columns, in this order: `nominee_id`, `nominee_name`, `total_wins`, `rank`.
- One row per nominee with at least one nomination where `won = 1`.
- `total_wins` is the number of that nominee's nominations with `won = 1`.
- `rank` follows `RANK()` over `total_wins` descending: equal totals share a rank and gaps follow a tie.
- Rows sorted by `total_wins` descending, then `nominee_id` ascending. `nominee_id` is unique, so the row order is fully determined.
### Example
Input `nominees`:
| nominee_id | nominee_name |
|---|---|
| 1 | Ava Lindqvist |
| 2 | Ben Okafor |
| 3 | Cara Mendes |
| 4 | Dan Whitlow |
| 5 | Eli Brandt |
Input `nominations`:
| nomination_id | nominee_id | year | category | won |
|---|---|---|---|---|
| 1 | 1 | 2015 | Best Actress | 1 |
| 2 | 1 | 2018 | Best Actress | 1 |
| 3 | 2 | 2016 | Best Director | 1 |
| 4 | 2 | 2019 | Best Picture | 1 |
| 5 | 3 | 2017 | Best Supporting Actress | 1 |
| 6 | 3 | 2020 | Best Actress | 0 |
| 7 | 4 | 2018 | Best Original Score | 0 |
| 8 | 5 | 2021 | Best Cinematography | 1 |
Expected output:
| nominee_id | nominee_name | total_wins | rank |
|---|---|---|---|
| 1 | Ava Lindqvist | 2 | 1 |
| 2 | Ben Okafor | 2 | 1 |
| 3 | Cara Mendes | 1 | 3 |
| 5 | Eli Brandt | 1 | 3 |
Ava and Ben each won twice and share rank 1, listed by `nominee_id`. Cara won one of her two nominations and Eli won his only one. Two nominees rank above them, so they share rank 3 and rank 2 is skipped. Dan's only nomination did not win, so he is left out.
### Constraints and Clarifications
- `nominees` has at most 10,000 rows and `nominations` at most 100,000.
- `won` is 0 or 1.
- Each nomination row with `won = 1` counts as one Oscar, including several wins in the same category in different years.
- Nominees whose nominations all lost, and nominees with no nominations, do not appear.
- `nominee_name` is not unique, so two different nominees with the same name stay as separate rows.
- Every `nominations.nominee_id` references an existing nominee.
Overview: Write a SQL query that ranks award nominees with at least one win by their total wins, where tied nominees share a rank and gaps follow each tie. It tests filtered aggregation, joins and the differences between RANK, DENSE_RANK and ROW_NUMBER.