Team Scores In Football Tournament Problem
Description
LeetCode Problem 1212.
Table: Teams
+---------------+----------+
| Column Name | Type |
+---------------+----------+
| team_id | int |
| team_name | varchar |
+---------------+----------+
team_id is the primary key of this table.
Each row of this table represents a single football team.
Table: Matches
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| match_id | int |
| host_team | int |
| guest_team | int |
| host_goals | int |
| guest_goals | int |
+---------------+---------+
match_id is the primary key of this table.
Each row is a record of a finished match between two different teams.
Teams host_team and guest_team are represented by their IDs in the teams table (team_id) and they scored host_goals and guest_goals goals respectively.
You would like to compute the scores of all teams after all matches. Points are awarded as follows:
- A team receives three points if they win a match (Score strictly more goals than the opponent team).
- A team receives one point if they draw a match (Same number of goals as the opponent team).
- A team receives no points if they lose a match (Score less goals than the opponent team).
Write an SQL query that selects the team_id, team_name and num_points of each team in the tournament after all described matches. Result table should be ordered by num_points (decreasing order). In case of a tie, order the records by team_id (increasing order).
The query result format is in the following example:
Teams table:
+-----------+--------------+
| team_id | team_name |
+-----------+--------------+
| 10 | Leetcode FC |
| 20 | NewYork FC |
| 30 | Atlanta FC |
| 40 | Chicago FC |
| 50 | Toronto FC |
+-----------+--------------+
Matches table:
+------------+--------------+---------------+-------------+--------------+
| match_id | host_team | guest_team | host_goals | guest_goals |
+------------+--------------+---------------+-------------+--------------+
| 1 | 10 | 20 | 3 | 0 |
| 2 | 30 | 10 | 2 | 2 |
| 3 | 10 | 50 | 5 | 1 |
| 4 | 20 | 30 | 1 | 0 |
| 5 | 50 | 30 | 1 | 0 |
+------------+--------------+---------------+-------------+--------------+
Result table:
+------------+--------------+---------------+
| team_id | team_name | num_points |
+------------+--------------+---------------+
| 10 | Leetcode FC | 7 |
| 20 | NewYork FC | 3 |
| 50 | Toronto FC | 3 |
| 30 | Atlanta FC | 1 |
| 40 | Chicago FC | 0 |
+------------+--------------+---------------+
Solution
Approach: Points per Team per Match, then LEFT JOIN
Each match gives points to two teams, so split it into two rows: one for the host and one for the guest, each with the points that team earned:
- 3 if the team scored more goals than the opponent,
- 1 for a draw,
- 0 for a loss.
UNION ALL stacks the host rows and the guest rows. Use UNION ALL, not UNION: two rows like (10, 3) from different matches are both real points, and UNION would collapse them into one.
Then start from Teams and LEFT JOIN these rows, so a team that played no matches (Chicago FC) still appears. Its SUM is NULL, which COALESCE(..., 0) turns into 0.
Sort by num_points descending, then by team_id ascending for ties.
The Pandas version builds the same host and guest rows with a small points helper, sums them per team, maps the totals onto teams with 0 for missing teams, and sorts by the same two keys.
WITH match_points AS (
-- One row per team per match, with the points that team earned.
SELECT host_team AS team_id,
CASE
WHEN host_goals > guest_goals THEN 3
WHEN host_goals = guest_goals THEN 1
ELSE 0
END AS points
FROM Matches
UNION ALL
SELECT guest_team AS team_id,
CASE
WHEN guest_goals > host_goals THEN 3
WHEN guest_goals = host_goals THEN 1
ELSE 0
END AS points
FROM Matches
)
SELECT t.team_id,
t.team_name,
COALESCE(SUM(mp.points), 0) AS num_points
FROM Teams AS t
LEFT JOIN match_points AS mp
ON mp.team_id = t.team_id
GROUP BY t.team_id, t.team_name
ORDER BY num_points DESC, t.team_id;