Mind the Gap

SQL practice problem 47 of 67 · Window Functions · Medium · 25 coins for your first accepted solve

Rank players by level descending (ties broken by name ascending).

Return name, level, and gap — how many levels below the player ranked directly above them they are. The top player's gap is NULL.

Return rows in rank order.

Schema

CREATE TABLE players (
  id         INT PRIMARY KEY,
  name       TEXT NOT NULL,
  country    TEXT,            -- NULL = unknown
  level      INT NOT NULL,
  joined_on  DATE NOT NULL,
  mentor_id  INT              -- another player's id, or NULL
);

Write your query in the browser, run it on the sample data, then submit it against hidden test data.

Previous: National Champions · Next: Running Tab · All problems