Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Mock2InteviewTournamentWinners.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Write your MySQL query statement below
WITH Playersinfo AS (
SELECT match_id, first_player AS player_id, first_score AS score FROM matches
UNION ALL
SELECT match_id, second_player AS player_id, second_score AS score FROm matches
),
PlayerGroup AS (
SELECT playersinfo.player_id, group_id, SUM(score) AS TotalScores
FROM Playersinfo
LEFT JOIN Players
ON Playersinfo.player_id = players.player_id
GROUP BY player_id, group_id
),
GroupRank AS (
SELECT player_id, group_id, DENSE_RANK() OVER (PARTITION BY group_id ORDER BY TotalScores DESC, player_id) AS GroupRanks
FROM PlayerGroup
)

SELECT group_id, player_id FROM GroupRank
WHERE GroupRanks = 1