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
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.
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code