Skip to main content

Video Game Matchmaking

MediumPremium

Electronic Arts makes popular online games that feature multiplayer “matchmaking”. For a special event, EA wants every player to play against every other player exactly once. Additionally, every player has a "level" that indicates their skill and experience in the game.

You're given a table players with the following columns:

  • player_name (string) – This column contains the username of the player.
  • level (integer) – This represents the skill level of the player, with higher numbers indicating greater skill.

Write a SQL query that generates all possible match-ups between the players in the players table and also calculates the disparity in level between the two players in each matchup. To keep the games evenly matched, we’d like to prevent matches where the players are more than 5 levels apart. Return the list of matches in ascending order of level_disparity.

Your output should contain the following columns: player1, player2 (both populated with player names), level_disparity

Just cross-joining the table will make it such that the players actually meet twice; once as player 1 and player 2, and once as player 2 and player 1.

To fix this, adjust your SQL query to only include combinations where the name of player1 is lexicographically less than the name of player2. This will ensure that each player pair is only included once.