Find the top-scoring player in each group from match results

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.

Wayfair logo
Wayfair
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

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.

Input Table(s)

players

ColumnTypeMeaning
player_idINTPrimary key
group_idINTGroup the player belongs to, NOT NULL

matches

ColumnTypeMeaning
match_idINTPrimary key
first_playerINTOne player in the match, references players.player_id, NOT NULL
second_playerINTThe other player, references players.player_id, NOT NULL
first_scoreINTPoints scored by first_player in this match, NOT NULL
second_scoreINTPoints 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_idgroup_id
110
210
310
420
520
630
730

matches

match_idfirst_playersecond_playerfirst_scoresecond_score
12131
23104
35422

Expected result:

group_idplayer_id
101
204
306

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.