1212. Team Scores in Football Tournament

https://leetcode.com/problems/team-scores-in-football-tournament

Description

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:

ac

Last updated

Was this helpful?