Find the top-scoring player in each group from match results
Company: Wayfair
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Each `matches` row is a finished match where `first_score` and `second_score` are the points of `first_player` and `second_player`. For each group, return the player with the maximum total points (players with no matches score 0, ties go to the lowest `player_id`). Return `group_id`, `player_id` ordered by `group_id` ascending.
```hint Two columns per player
A player can appear in either player column, so their total draws on two score columns. Check what your join does to a player who never played.
```
### Input Table(s)
`players`
| Column | Type | Meaning |
|---|---|---|
| player_id | INT | Primary key |
| group_id | INT | Group the player belongs to, NOT NULL |
`matches`
| Column | Type | Meaning |
|---|---|---|
| match_id | INT | Primary key |
| first_player | INT | One player in the match, references `players.player_id`, NOT NULL |
| second_player | INT | The other player, references `players.player_id`, NOT NULL |
| first_score | INT | Points scored by `first_player` in this match, NOT NULL |
| second_score | INT | Points scored by `second_player` in this match, NOT NULL |
### Output Contract
- Columns, in this order: `group_id`, `player_id`.
- Exactly one row per group.
- `player_id` is the player with the highest total points in that group. A player's total is the sum of `first_score` over the matches where they are `first_player` plus the sum of `second_score` over the matches where they are `second_player`.
- Ties on total points go to the lowest `player_id`.
- Order by `group_id` ascending.
### Example
`players`
| player_id | group_id |
|---|---|
| 1 | 10 |
| 2 | 10 |
| 3 | 10 |
| 4 | 20 |
| 5 | 20 |
| 6 | 30 |
| 7 | 30 |
`matches`
| match_id | first_player | second_player | first_score | second_score |
|---|---|---|---|---|
| 1 | 2 | 1 | 3 | 1 |
| 2 | 3 | 1 | 0 | 4 |
| 3 | 5 | 4 | 2 | 2 |
Expected result:
| group_id | player_id |
|---|---|
| 10 | 1 |
| 20 | 4 |
| 30 | 6 |
In group 10, player 1 was the second player in both matches and scored 1 + 4 = 5, player 2 scored 3 and player 3 scored 0, so player 1 wins. In group 20, players 4 and 5 both have 2 points, and the tie goes to the lower id, 4. Nobody in group 30 played, so both players have 0 and player 6 wins on the lower id.
### Constraints and Clarifications
- There are between 1 and 10,000 players.
- Each group has at least one player, and each player belongs to exactly one group.
- `first_player` is never equal to `second_player`, and every player in `matches` exists in `players`.
- No column contains NULL.
- Players with no matches have a total of 0 and still count. A group where nobody played returns its lowest `player_id`.
- A player's total counts every match they played, whichever group the opponent belongs to.
Overview: Write a SQL query that totals each player's points across both player columns of a matches table and returns the top scorer in every group, with ties going to the lowest player ID. It tests conditional aggregation, outer joins that keep players with no matches, and ranking with window functions.