Compute each team's points in a football tournament from match results

Quick Overview

Write a SQL query that totals each team's points in a football tournament, with 3 for a win, 1 for a draw and 0 for a loss, whether the team played as host or guest. It tests conditional aggregation, outer joins that keep teams with no matches, and deterministic tie ordering.

Compute each team's points in a football tournament from match results

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

A football tournament records its teams and every match played. Write a SQL query returning `team_id`, `team_name` and `num_points` for every team after all matches. Points are awarded per match: - Win (more goals than the opponent): 3 points. - Draw (same number of goals): 1 point. - Loss: 0 points. Teams with no matches appear with 0 points. Order by `num_points` descending, ties by `team_id` ascending. ```hint Two roles per team A team can appear in `Matches` as `host_team` or as `guest_team`. Check that each match adds points to both of its teams and that a team with no matches still produces a row. ``` ### Input Table(s) `Teams` | Column | Type | Meaning | |---|---|---| | team_id | INT | Team id | | team_name | VARCHAR(50) | Team name | Primary key: `team_id`. `Matches` | Column | Type | Meaning | |---|---|---| | match_id | INT | Match id | | host_team | INT | `team_id` of the host team | | guest_team | INT | `team_id` of the guest team | | host_goals | INT | Goals scored by the host team | | guest_goals | INT | Goals scored by the guest team | Primary key: `match_id`. `host_team` and `guest_team` both reference `Teams.team_id`. ### Output Contract - Columns, in this order: `team_id`, `team_name`, `num_points`. - Exactly one row per team in `Teams`, including teams that played no match (`num_points` = 0). - `num_points` is an integer, the sum of the points the team earned across all its matches. - Order by `num_points` descending, then `team_id` ascending. `team_id` is the primary key, so the order is unique. ### Example `Teams` | team_id | team_name | |---|---| | 10 | Lions | | 20 | Tigers | | 30 | Bears | | 40 | Wolves | | 50 | Eagles | `Matches` | 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 | Expected result: | team_id | team_name | num_points | |---|---|---| | 10 | Lions | 7 | | 20 | Tigers | 3 | | 50 | Eagles | 3 | | 30 | Bears | 1 | | 40 | Wolves | 0 | Lions win matches 1 and 3 and draw match 2 as the guest, for 7 points. Tigers and Eagles each have one win and one loss, so they tie on 3 and the lower `team_id` comes first. Bears have one draw and two losses, and Wolves played no match. ### Constraints and Clarifications - `Teams` has 1 to 100 rows and `Matches` has 0 to 100 rows. - `host_team` and `guest_team` always reference existing teams, and `host_team != guest_team`. - `host_goals` and `guest_goals` are non-NULL for every match. - The same two teams can meet more than once. Every match counts. - Team names are not guaranteed to be unique, so results are per `team_id`.

Overview: Write a SQL query that totals each team's points in a football tournament, with 3 for a win, 1 for a draw and 0 for a loss, whether the team played as host or guest. It tests conditional aggregation, outer joins that keep teams with no matches, and deterministic tie ordering.

PracHub logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

A football tournament records its teams and every match played. Write a SQL query returning team_id, team_name and num_points for every team after all matches.

Points are awarded per match:

  • Win (more goals than the opponent): 3 points.
  • Draw (same number of goals): 1 point.
  • Loss: 0 points.

Teams with no matches appear with 0 points. Order by num_points descending, ties by team_id ascending.

Input Table(s)

Teams

ColumnTypeMeaning
team_idINTTeam id
team_nameVARCHAR(50)Team name

Primary key: team_id.

Matches

ColumnTypeMeaning
match_idINTMatch id
host_teamINTteam_id of the host team
guest_teamINTteam_id of the guest team
host_goalsINTGoals scored by the host team
guest_goalsINTGoals scored by the guest team

Primary key: match_id. host_team and guest_team both reference Teams.team_id.

Output Contract

  • Columns, in this order: team_id , team_name , num_points .
  • Exactly one row per team in Teams , including teams that played no match ( num_points = 0).
  • num_points is an integer, the sum of the points the team earned across all its matches.
  • Order by num_points descending, then team_id ascending. team_id is the primary key, so the order is unique.

Example

Teams

team_idteam_name
10Lions
20Tigers
30Bears
40Wolves
50Eagles

Matches

match_idhost_teamguest_teamhost_goalsguest_goals
1102030
2301022
3105051
4203010
5503010

Expected result:

team_idteam_namenum_points
10Lions7
20Tigers3
50Eagles3
30Bears1
40Wolves0

Lions win matches 1 and 3 and draw match 2 as the guest, for 7 points. Tigers and Eagles each have one win and one loss, so they tie on 3 and the lower team_id comes first. Bears have one draw and two losses, and Wolves played no match.

Constraints and Clarifications

  • Teams has 1 to 100 rows and Matches has 0 to 100 rows.
  • host_team and guest_team always reference existing teams, and host_team != guest_team .
  • host_goals and guest_goals are non-NULL for every match.
  • The same two teams can meet more than once. Every match counts.
  • Team names are not guaranteed to be unique, so results are per team_id .