Quick 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.

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.

Each row of `matches` is a finished match where `first_score` and `second_score` are the points scored by `first_player` and `second_player`, and `players` lists every player with the `group_id` of the group they belong to. 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`. Every match a player played counts toward their total, whichever group the opponent belongs to, and a player with no matches has a total of 0 and still counts. For each group, return the player with the highest total points. Ties on total go to the lowest `player_id`, so a group where nobody played returns its lowest `player_id`. Return exactly one row per group with the columns `group_id`, `player_id`, ordered by `group_id` ascending. Each group has at least one player, each player belongs to exactly one group, `first_player` never equals `second_player`, every player in `matches` exists in `players`, and no column contains NULL.

Tables

players(player_id INTEGER, group_id INTEGER)

matches(match_id INTEGER, first_player INTEGER, second_player INTEGER, first_score INTEGER, second_score INTEGER)

Hints

  1. 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.
  2. A player who never played has a total of 0, and ties on total go to the lowest player_id, including in a group where nobody played.
WayfairData ScientistmediumData Manipulation (SQL/Python)

Top Scorer in Each Group

Each row of matches is a finished match where first_score and second_score are the points scored by first_player and second_player, and players lists every player with the group_id of the group they belong to. 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. Every match a player played counts toward their total, whichever group the opponent belongs to, and a player with no matches has a total of 0 and still counts. For each group, return the player with the highest total points. Ties on total go to the lowest player_id, so a group where nobody played returns its lowest player_id. Return exactly one row per group with the columns group_id, player_id, ordered by group_id ascending. Each group has at least one player, each player belongs to exactly one group, first_player never equals second_player, every player in matches exists in players, and no column contains NULL.

Expected Output

Tables

playersmatches
players
ColumnType
player_idINTEGER
group_idINTEGER
matches
ColumnType
match_idINTEGER
first_playerINTEGER
second_playerINTEGER
first_scoreINTEGER
second_scoreINTEGER

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions