SQL interview question: Rank scores without gaps

medium
DENSE_RANK
ORDER BY

Return every score with its rank, highest first. Equal scores share a rank and the next rank has no gap (1, 2, 2, 3). Order by score descending.

Expected columns: score, rank. Column names don't have to match. Runs on SQLite in your browser.

Tables: scores

CREATE TABLE scores (
  id INTEGER PRIMARY KEY,
  player TEXT NOT NULL,
  score REAL NOT NULL
);
INSERT INTO scores VALUES
  (1, 'ana', 3.50), (2, 'bo', 3.65), (3, 'cy', 4.00),
  (4, 'di', 3.85), (5, 'ed', 4.00), (6, 'fe', 3.65);

Your query

Ctrl/Cmd + Enter

FAQ