cswork / libraryOpen practice workspace

Coding questions · Atlassian

Rank each team’s top 3 scores in 2024

This question evaluates data manipulation and relational-query competencies, focusing on per-entity ranking, deterministic tie-breaking, and reshaping game-level records into team-centric perspectives.

Using the same schema, compute each team’s top 3 highest-scoring games for season_year = 2024, treating each game from BOTH teams’ perspectives (i.e., if A vs B is one row, also consider B’s score in that game). Return columns: team_name, opponent_team_name, team_score, score_rank (1 = highest per team). Break ties deterministically by: team_score DESC, date DESC, game_id ASC. Schema: team(team_id INT PRIMARY KEY, team_name TEXT); game(game_id INT PRIMARY KEY, team_id INT, opponent_team_id INT, date DATE, season_year INT, team_score INT, opponent_team_score INT, game_type TEXT). Sample data: team

+---------+------------------------------+
| team_id | team_name |
+---------+------------------------------+
| 1 | UConn Huskies |
| 2 | San Diego State Aztecs |
| 3 | Alabama Crimson Tide |
| 4 | Purdue Boilermakers |
+---------+------------------------------+
game
+---------+------------+-------------+---------+-------------------+------------+---------------------+---------------+
| game_id | date | season_year | team_id | opponent_team_id | team_score | opponent_team_score | game_type |
+---------+------------+-------------+---------+-------------------+------------+---------------------+---------------+
| 1101 | 2024-01-15 | 2024 | 3 | 4 | 88 | 79 | REGULAR |
| 1102 | 2024-02-01 | 2024 | 1 | 3 | 95 | 70 | REGULAR |
| 1103 | 2024-02-20 | 2024 | 1 | 4 | 82 | 85 | REGULAR |
| 1104 | 2024-03-03 | 2024 | 2 | 1 | 91 | 93 | REGULAR |
| 1105 | 2024-03-10 | 2024 | 2 | 3 | 77 | 74 | REGULAR |
+---------+------------+-------------+---------+-------------------+------------+---------------------+---------------+

Write a single SQL query (CTEs allowed) that produces the required columns with row_number-based ranking.

Rank each team’s top 3 scores in 2024

Table team

columntypeconstraints
team_idINTPRIMARY KEY
team_nameVARCHAR(100)NOT NULL

Sample data:

team_idteam_name
1UConn Huskies
2San Diego State Aztecs
3Alabama Crimson Tide
4Purdue Boilermakers

Table game

columntypeconstraints
game_idINTPRIMARY KEY
team_idINTNOT NULL
opponent_team_idINTNOT NULL
dateDATENOT NULL
season_yearINTNOT NULL
team_scoreINTNOT NULL
opponent_team_scoreINTNOT NULL
game_typeVARCHAR(20)NOT NULL

Sample data:

dategame_idgame_typeopponent_team_idopponent_team_scoreseason_yearteam_idteam_score
2024-01-151101REGULAR4792024388
2024-02-011102REGULAR3702024195
2024-02-201103REGULAR4852024182
2024-03-031104REGULAR1932024291
2024-03-101105REGULAR3742024277

Expected output

For each team, list up to their top 3 games (by points scored in 2024), considering both when they appear as team_id and as opponent_team_id. Each row includes the team’s name, their opponent’s name, the points that team scored in that game, and a score-based rank per team (1 = highest score). Ties are broken by team_score DESC, date DESC, game_id ASC.

opponent_team_namescore_rankteam_nameteam_score
Purdue Boilermakers1Alabama Crimson Tide88
San Diego State Aztecs2Alabama Crimson Tide74
UConn Huskies3Alabama Crimson Tide70
UConn Huskies1Purdue Boilermakers85
Alabama Crimson Tide2Purdue Boilermakers79
UConn Huskies1San Diego State Aztecs91
Alabama Crimson Tide2San Diego State Aztecs77
Alabama Crimson Tide1UConn Huskies95
San Diego State Aztecs2UConn Huskies93
Purdue Boilermakers3UConn Huskies82

Hints

  1. Think about transforming each game into two rows: one for each team’s perspective.
  2. Use ROW_NUMBER() partitioned by team_id with the specified ORDER BY to rank scores per team, then filter to the top 3.