Write a query to print the matches played between teams in a round‑robin tournament.
💡 Model Answer
Assuming a table matches with columns match_id, team_a_id, team_b_id, and match_date, and a teams table with team_id and team_name, you can list all round‑robin matches with:
SELECT
ta.team_name AS team_a,
tb.team_name AS team_b,
m.match_date
FROM
matches m
JOIN teams ta ON m.team_a_id = ta.team_id
JOIN teams tb ON m.team_b_id = tb.team_id
ORDER BY m.match_date;If you only have a list of team IDs and want to generate every unique pair (i.e., the round‑robin schedule) without a pre‑filled matches table, you can self‑join the teams table:
SELECT
t1.team_name AS team_a,
t2.team_name AS team_b
FROM
teams t1
JOIN teams t2 ON t1.team_id < t2.team_id
ORDER BY t1.team_name, t2.team_name;The t1.team_id < t2.team_id condition ensures each pair appears only once. Complexity is O(n²) for n teams, which is expected for generating all pairings in a round‑robin format.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.
Get Assisting AI — Starts at ₹500