cswork / libraryOpen practice workspace

Coding questions · Atlassian

Find 2023 NCAA championship winner

This question evaluates proficiency in SQL data manipulation and relational reasoning, testing the ability to identify a championship winner from game result records across related tables in the Data Manipulation (SQL/Python) domain for data scientist roles.

You are given two tables. 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 |
+---------+------------+-------------+---------+-------------------+------------+---------------------+---------------+
| 1001 | 2023-04-03 | 2023 | 1 | 2 | 76 | 59 | CHAMPIONSHIP |
| 1101 | 2024-01-15 | 2024 | 3 | 4 | 88 | 79 | REGULAR |
+---------+------------+-------------+---------+-------------------+------------+---------------------+---------------+

Write a single SQL query to return the team_name of the 2023 CHAMPIONSHIP winner. Requirements: (1) Determine the winner even if the champion is stored either as team_id or opponent_team_id. (2) Assume no ties in CHAMPIONSHIP; if a tie exists due to data issues, break by higher score. (3) Use only the two tables and no hard-coded team ids.

Find the 2023 NCAA Championship Winner

You are given two tables:

team(team_id INT PRIMARY KEY, team_name VARCHAR(100))

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 VARCHAR(50))

Write a single SQL query to return the team_name of the 2023 CHAMPIONSHIP winner.

Requirements:

  1. Correctly determine the winner even if the champion is stored either as team_id or opponent_team_id.
  2. Assume no ties in CHAMPIONSHIP games; if a tie exists due to data issues, break it by picking the team with the higher score.
  3. Use only the two tables and do not hard-code any specific team ids.

Use the schema and sample data below.

Table team

columntypeconstraints
team_idINTPRIMARY KEY, NOT NULL
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, NOT NULL
dateDATENOT NULL
season_yearINTNOT NULL
team_idINTNOT NULL
opponent_team_idINTNOT NULL
team_scoreINTNOT NULL
opponent_team_scoreINTNOT NULL
game_typeVARCHAR(50)NOT NULL

Sample data:

dategame_idgame_typeopponent_team_idopponent_team_scoreseason_yearteam_idteam_score
2023-04-031001CHAMPIONSHIP2592023176
2024-01-151101REGULAR4792024388

Expected output

Return a single row containing the team_name of the 2023 CHAMPIONSHIP winner.

team_name
UConn Huskies

Hints

  1. Treat team_id and opponent_team_id as separate rows with their respective scores (UNION ALL).
  2. Once each team-score pair is on its own row, filter to 2023 CHAMPIONSHIP games and pick the highest score.