SQL exercise · about 5 minutes · free
Window Functions: Best Match Per Player
Window functions are the most-loved SQL feature in interviews and the engine behind 'keep the latest row' dedupe.
ROW_NUMBER() OVER (PARTITION BY x ORDER BY y DESC) numbers rows within each group. Keep number 1 to get the top row per group.
SELECT * FROM (
SELECT player_id, id,
ROW_NUMBER() OVER (PARTITION BY player_id ORDER BY score DESC) AS rn
FROM matches
) WHERE rn = 1Your quest: For each player, show
player_id and the id of their highest-scoring match.Starter code
-- use ROW_NUMBER() OVER (PARTITION BY ...) -- Run this first to see the matches table, then change it to answer the task. SELECT * FROM matches LIMIT 5;