Premier League Table Ranking II - Problem

โšฝ Premier League Table Ranking II

You are the data analyst for the Premier League! Your task is to calculate the official league table with points, positions, and tier rankings for all teams.

๐Ÿ“Š Points System

  • 3 points for a win ๐Ÿ†
  • 1 point for a draw โš–๏ธ
  • 0 points for a loss โŒ

๐Ÿ… Position Rules

Teams are ranked by points (highest first). Teams with identical points share the same position.

๐ŸŽฏ Tier Classification

Divide all teams into 3 performance tiers:

  • Tier 1: Top 33% of teams (Champions League contenders)
  • Tier 2: Middle 33% of teams (Europa League zone)
  • Tier 3: Bottom 34% of teams (Relegation battle)

Important: When teams tie at tier boundaries, they go to the higher tier.

๐Ÿ“‹ Output Format

Return results ordered by: points DESC, then team_name ASC

Input & Output

example_1.sql โ€” Premier League Table
$ Input: TeamStats: +---------+-------------------+----------------+------+-------+--------+ | team_id | team_name | matches_played | wins | draws | losses | +---------+-------------------+----------------+------+-------+--------+ | 8 | Sheffield United | 20 | 18 | 2 | 0 | | 5 | Fulham | 31 | 18 | 1 | 12 | | 7 | Newcastle United | 33 | 11 | 10 | 12 | | 1 | Chelsea | 22 | 13 | 2 | 7 | +---------+-------------------+----------------+------+-------+--------+
โ€บ Output: +-------------------+--------+----------+---------+ | team_name | points | position | tier | +-------------------+--------+----------+---------+ | Sheffield United | 56 | 1 | Tier 1 | | Fulham | 55 | 2 | Tier 1 | | Newcastle United | 43 | 3 | Tier 1 | | Chelsea | 41 | 4 | Tier 1 | +-------------------+--------+----------+---------+
๐Ÿ’ก Note: Sheffield United leads with 56 points (18ร—3 + 2ร—1), followed by Fulham with 55 points (18ร—3 + 1ร—1). All teams shown are in Tier 1 as they're the top performers.
example_2.sql โ€” Tied Teams
$ Input: TeamStats: +---------+-------------+----------------+------+-------+--------+ | team_id | team_name | matches_played | wins | draws | losses | +---------+-------------+----------------+------+-------+--------+ | 10 | Everton | 14 | 2 | 6 | 6 | | 9 | Luton Town | 5 | 4 | 0 | 1 | +---------+-------------+----------------+------+-------+--------+
โ€บ Output: +-------------+--------+----------+---------+ | team_name | points | position | tier | +-------------+--------+----------+---------+ | Everton | 12 | 1 | Tier 1 | | Luton Town | 12 | 1 | Tier 1 | +-------------+--------+----------+---------+
๐Ÿ’ก Note: Both teams have 12 points: Everton (2ร—3 + 6ร—1) and Luton Town (4ร—3 + 0ร—1). They share position 1. When ordered by team_name, Everton comes first alphabetically.
example_3.sql โ€” Single Team Edge Case
$ Input: TeamStats: +---------+-----------+----------------+------+-------+--------+ | team_id | team_name | matches_played | wins | draws | losses | +---------+-----------+----------------+------+-------+--------+ | 1 | Arsenal | 10 | 5 | 3 | 2 | +---------+-----------+----------------+------+-------+--------+
โ€บ Output: +-----------+--------+----------+---------+ | team_name | points | position | tier | +-----------+--------+----------+---------+ | Arsenal | 18 | 1 | Tier 1 | +-----------+--------+----------+---------+
๐Ÿ’ก Note: Arsenal has 18 points (5ร—3 + 3ร—1). With only one team, it's automatically position 1 and Tier 1 (top 33%).

Constraints

  • 1 โ‰ค number of teams โ‰ค 100
  • 1 โ‰ค team_id โ‰ค 1000
  • 1 โ‰ค team_name.length โ‰ค 50
  • 0 โ‰ค wins, draws, losses โ‰ค 50
  • matches_played = wins + draws + losses
  • Team names are unique

Visualization

Tap to expand
PREMIER LEAGUE TABLETIER 1 - CHAMPIONS LEAGUE1. Sheffield United - 56 pts (18W-2D-0L)2. Fulham - 55 pts (18W-1D-12L)3. Newcastle United - 43 pts (11W-10D-12L)TIER 2 - EUROPA LEAGUE5. Burnley - 27 pts (6W-9D-11L)6. Nottingham Forest - 24 pts (6W-6D-15L)TIER 3 - RELEGATION BATTLE9. Liverpool - 11 pts (1W-8D-8L)10. Aston Villa - 9 pts (1W-6D-13L)๐Ÿ†๐Ÿฅˆโš ๏ธ
Understanding the Visualization
1
Match Results Input
Each team's wins, draws, and losses are recorded
2
Points Calculation
System calculates: Points = Wins ร— 3 + Draws ร— 1
3
Ranking Application
Teams are ranked by points (descending), then by name (ascending)
4
Tier Assignment
Top 33% โ†’ Tier 1, Middle 33% โ†’ Tier 2, Bottom 34% โ†’ Tier 3
Key Takeaway
๐ŸŽฏ Key Insight: Window functions like RANK() and NTILE() provide elegant solutions for complex ranking scenarios with ties and percentage-based groupings, all in a single database operation.
Asked in
ESPN 25 Sky Sports 18 FanDuel 15 DraftKings 12
28.4K Views
Medium Frequency
~18 min Avg. Time
892 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