Skip to content

3322. Premier League Table Ranking III 👍

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
WITH
  TeamPoints AS (
    SELECT
      season_id,
      team_id,
      team_name,
      wins * 3 + draws AS points,
      goals_for - goals_against AS goal_difference
    FROM SeasonStats
  ),
  RankedTeams AS (
    SELECT
      *,
      RANK() OVER(
        PARTITION BY season_id
        ORDER BY points DESC, goal_difference DESC, team_name
      ) AS position
    FROM TeamPoints
  )
SELECT * FROM RankedTeams
ORDER BY season_id, position, team_name;