All the Matches of the League - Problem
All the Matches of the League

You are tasked with organizing a sports league where every team must play against every other team exactly twice - once as the home team and once as the away team. This creates a complete round-robin tournament format.

Given a table Teams with team names, you need to generate all possible match combinations:

Teams table:
Column NameType
team_namevarchar

Your task:
• Generate all possible matches where each pair of teams plays twice
• Each team should be home_team once and away_team once against every other team
• Return the matches in any order

Example: If teams are [A, B, C], the matches would be:
A vs B (A home), B vs A (B home), A vs C (A home), C vs A (C home), B vs C (B home), C vs B (C home)

This is a classic Cartesian product problem with a self-exclusion condition!

Input & Output

example_1.sql — Basic League
$ Input: Teams table: | team_name | |----------| | Arsenal | | Chelsea | | Liverpool |
Output: | home_team | away_team | |-----------|----------| | Arsenal | Chelsea | | Arsenal | Liverpool | | Chelsea | Arsenal | | Chelsea | Liverpool | | Liverpool | Arsenal | | Liverpool | Chelsea |
💡 Note: Each team plays against every other team once as home and once as away. With 3 teams, we get 3×2 = 6 total matches.
example_2.sql — Two Teams Only
$ Input: Teams table: | team_name | |----------| | TeamA | | TeamB |
Output: | home_team | away_team | |-----------|----------| | TeamA | TeamB | | TeamB | TeamA |
💡 Note: With only 2 teams, each team plays the other once at home and once away, resulting in 2 matches total.
example_3.sql — Single Team Edge Case
$ Input: Teams table: | team_name | |----------| | OnlyTeam |
Output: Empty result set (no matches possible)
💡 Note: A single team cannot play against itself, so no matches are generated. The WHERE clause filters out the self-match.

Visualization

Tap to expand
League Match Generation ProcessAArsenalCChelseaLLiverpoolCROSS JOIN ✕All Possible Matches (6 total)Arsenal → ChelseaChelsea → ArsenalArsenal → LiverpoolLiverpool → ArsenalChelsea → LiverpoolLiverpool → ChelseaFormulaTotal Matches:n × (n - 1)where n = number of teams
Understanding the Visualization
1
Cross Join Teams
Create all possible combinations by joining Teams table with itself
2
Filter Self-Matches
Remove invalid matches where a team would play against itself
3
Generate Schedule
Result contains complete home/away match schedule for the league
Key Takeaway
🎯 Key Insight: Use CROSS JOIN to get all team combinations, then filter out self-matches with WHERE clause to create a complete round-robin schedule

Time & Space Complexity

Time Complexity
⏱️
O(n²)

Cross join creates n² combinations, where n is the number of teams

n
2n
Quadratic Growth
Space Complexity
O(n²)

Result set contains n×(n-1) matches for n teams

n
2n
Quadratic Space

Constraints

  • 1 ≤ number of teams ≤ 1000
  • Team names are unique varchar strings
  • Each team name length ≤ 100 characters
  • No duplicate team names in the input table
Asked in
Amazon 25 Microsoft 18 Google 15 Meta 12
28.5K Views
Medium Frequency
~8 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