Premier League Table Ranking - Problem

You are tasked with creating a Premier League table ranking system that automatically calculates points and ranks teams based on their match results.

Given a database table TeamStats containing team statistics, you need to:

  • Calculate total points for each team using Premier League scoring rules
  • Rank teams based on their points
  • Handle ties correctly (same points = same rank)
  • Sort results by points (descending) and team name (ascending)

Scoring System:

  • πŸ† Win = 3 points
  • 🀝 Draw = 1 point
  • ❌ Loss = 0 points

The table structure is:

+------------------+---------+
| Column Name      | Type    |
+------------------+---------+
| team_id          | int     |
| team_name        | varchar |
| matches_played   | int     |
| wins             | int     |
| draws            | int     |
| losses           | int     |
+------------------+---------+

Note: Teams with identical points must receive the same rank number.

Input & Output

example_1.sql β€” Basic League Table
$ Input: TeamStats: +----------+-----------+----------------+------+-------+--------+ | team_id | team_name | matches_played | wins | draws | losses | +----------+-----------+----------------+------+-------+--------+ | 1 | Arsenal | 20 | 15 | 3 | 2 | | 2 | Chelsea | 20 | 12 | 5 | 3 | | 3 | Liverpool | 20 | 10 | 8 | 2 | +----------+-----------+----------------+------+-------+--------+
β€Ί Output: +----------+-----------+--------+------+ | team_id | team_name | points | rank | +----------+-----------+--------+------+ | 1 | Arsenal | 48 | 1 | | 3 | Liverpool | 38 | 2 | | 2 | Chelsea | 41 | 3 | +----------+-----------+--------+------+
πŸ’‘ Note: Arsenal: 15Γ—3 + 3Γ—1 = 48 points (Rank 1), Liverpool: 10Γ—3 + 8Γ—1 = 38 points (Rank 2), Chelsea: 12Γ—3 + 5Γ—1 = 41 points gets sorted between Arsenal and Liverpool but ranks as 3rd when ordered by points DESC, team_name ASC
example_2.sql β€” Teams with Equal Points
$ Input: TeamStats: +----------+-----------+----------------+------+-------+--------+ | team_id | team_name | matches_played | wins | draws | losses | +----------+-----------+----------------+------+-------+--------+ | 1 | Barcelona | 15 | 10 | 2 | 3 | | 2 | Madrid | 15 | 8 | 8 | -1 | | 3 | Atletico | 15 | 8 | 8 | -1 | +----------+-----------+----------------+------+-------+--------+
β€Ί Output: +----------+-----------+--------+------+ | team_id | team_name | points | rank | +----------+-----------+--------+------+ | 1 | Barcelona | 32 | 1 | | 3 | Atletico | 32 | 2 | | 2 | Madrid | 32 | 2 | +----------+-----------+--------+------+
πŸ’‘ Note: Barcelona: 10Γ—3 + 2Γ—1 = 32 points (Rank 1), Both Atletico and Madrid: 8Γ—3 + 8Γ—1 = 32 points each (both Rank 2). When points are tied, teams get the same rank, and results are ordered by team_name alphabetically (Atletico before Madrid).
example_3.sql β€” Single Team Edge Case
$ Input: TeamStats: +----------+-----------+----------------+------+-------+--------+ | team_id | team_name | matches_played | wins | draws | losses | +----------+-----------+----------------+------+-------+--------+ | 1 | Juventus | 10 | 0 | 0 | 10 | +----------+-----------+----------------+------+-------+--------+
β€Ί Output: +----------+-----------+--------+------+ | team_id | team_name | points | rank | +----------+-----------+--------+------+ | 1 | Juventus | 0 | 1 | +----------+-----------+--------+------+
πŸ’‘ Note: Even with 0 wins, 0 draws, and 10 losses (0Γ—3 + 0Γ—1 + 10Γ—0 = 0 points), Juventus still gets Rank 1 as they are the only team in the table.

Constraints

  • 1 ≀ number of teams ≀ 1000
  • 0 ≀ wins, draws, losses ≀ 50
  • matches_played = wins + draws + losses
  • team_name contains only letters and spaces
  • Points calculation: wins Γ— 3 + draws Γ— 1 + losses Γ— 0

Visualization

Tap to expand
Premier League Table RankingπŸ“Š Step 1: Calculate PointsArsenal: 15 wins Γ— 3pts + 3 draws Γ— 1pt + 2 losses Γ— 0pt = 48 pointsChelsea: 12 wins Γ— 3pts + 5 draws Γ— 1pt + 3 losses Γ— 0pt = 41 pointsLiverpool: 10 wins Γ— 3pts + 8 draws Γ— 1pt + 2 losses Γ— 0pt = 38 pointsπŸ”„ Step 2: Sort TeamsSorted by: Points DESC (48 β†’ 41 β†’ 38), then Team Name ASCResult Order: Arsenal (48) β†’ Chelsea (41) β†’ Liverpool (38)πŸ† Step 3: Assign Ranks1Arsenal - 48 points2Chelsea - 41 points3Liverpool - 38 pointsπŸ’‘ Tie HandlingIf teams have equal points:β€’ Same rank assignedβ€’ Next rank number skipped
Understanding the Visualization
1
Calculate Team Points
For each team: Points = WinsΓ—3 + DrawsΓ—1 + LossesΓ—0
2
Sort by Performance
Order teams by points (highest first), then alphabetically by name for ties
3
Assign Rankings
Use RANK() to give same rank to teams with equal points, skip next ranks
Key Takeaway
🎯 Key Insight: SQL's RANK() window function efficiently handles tie scenarios and maintains proper ranking gaps, making it the optimal choice for leaderboard calculations.
Asked in
ESPN 25 DraftKings 18 FanDuel 15 Sportradar 12
28.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen