Given a SeasonStats table containing Premier League season statistics for teams, write a solution to calculate the points, goal difference, and position for each team in each season.
Ranking Rules:
- Teams are first ranked by their total points (highest to lowest)
- If points are tied, teams are ranked by goal difference (highest to lowest)
- If goal difference is also tied, teams are ranked alphabetically by team name
Points Calculation:
- 3 points for a win
- 1 point for a draw
- 0 points for a loss
Goal Difference: goals_for - goals_against
Return the result table ordered by season_id ascending, then by position ascending, and finally by team_name ascending.
Table Schema
| Column Name | Type | Description |
|---|---|---|
season_id
PK
|
int | Season identifier |
team_id
PK
|
int | Team identifier |
team_name
|
varchar | Team name |
matches_played
|
int | Number of matches played |
wins
|
int | Number of wins |
draws
|
int | Number of draws |
losses
|
int | Number of losses |
goals_for
|
int | Goals scored by the team |
goals_against
|
int | Goals conceded by the team |
Input & Output
| season_id | team_id | team_name | matches_played | wins | draws | losses | goals_for | goals_against |
|---|---|---|---|---|---|---|---|---|
| 2021 | 1 | Manchester City | 38 | 29 | 6 | 3 | 99 | 26 |
| 2021 | 2 | Liverpool | 38 | 28 | 8 | 2 | 94 | 26 |
| 2021 | 3 | Chelsea | 38 | 21 | 11 | 6 | 76 | 33 |
| 2022 | 1 | Manchester City | 38 | 28 | 5 | 5 | 94 | 33 |
| 2022 | 2 | Arsenal | 38 | 26 | 6 | 6 | 88 | 43 |
| season_id | team_id | team_name | points | goal_difference | position |
|---|---|---|---|---|---|
| 2021 | 1 | Manchester City | 93 | 73 | 1 |
| 2021 | 2 | Liverpool | 92 | 68 | 2 |
| 2021 | 3 | Chelsea | 74 | 43 | 3 |
| 2022 | 1 | Manchester City | 89 | 61 | 1 |
| 2022 | 2 | Arsenal | 84 | 45 | 2 |
For 2021 season: Manchester City has 93 points (29×3 + 6×1) and goal difference of 73 (99-26), ranking 1st. Liverpool has 92 points (28×3 + 8×1) and goal difference of 68 (94-26), ranking 2nd. For 2022 season: Manchester City leads with 89 points and +61 goal difference, followed by Arsenal with 84 points and +45 goal difference.
| season_id | team_id | team_name | matches_played | wins | draws | losses | goals_for | goals_against |
|---|---|---|---|---|---|---|---|---|
| 2023 | 1 | Arsenal | 10 | 6 | 3 | 1 | 25 | 15 |
| 2023 | 2 | Chelsea | 10 | 7 | 0 | 3 | 20 | 12 |
| 2023 | 3 | Brighton | 10 | 7 | 0 | 3 | 18 | 10 |
| season_id | team_id | team_name | points | goal_difference | position |
|---|---|---|---|---|---|
| 2023 | 1 | Arsenal | 21 | 10 | 1 |
| 2023 | 3 | Brighton | 21 | 8 | 2 |
| 2023 | 2 | Chelsea | 21 | 8 | 3 |
All three teams have 21 points, so ranking goes by goal difference. Arsenal (+10) ranks 1st. Brighton and Chelsea both have +8 goal difference, so they are ranked alphabetically: Brighton (2nd), Chelsea (3rd).
Constraints
-
1 ≤ season_id ≤ 2030 -
1 ≤ team_id ≤ 100 -
1 ≤ team_name.length ≤ 50 -
matches_played = wins + draws + losses -
0 ≤ wins, draws, losses ≤ 50 -
0 ≤ goals_for, goals_against ≤ 200