Rank Scores Highest to Lowest With Shared Ranks for Ties and No Gaps

Quick Overview

Write a SQL query that ranks every score from highest to lowest, so that equal scores share a rank and the next lower score takes the next integer with no gaps. It tests window ranking functions, how ties are numbered, and ordering the final result explicitly.

Rank Scores Highest to Lowest With Shared Ranks for Ties and No Gaps

Company: NVIDIA

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Rank the scores in `Scores` from highest to lowest, returning `score` and `rank`. Ties share the same rank, and the next rank after a tie is the next consecutive integer (no gaps). Order by `score` descending. Every row of `Scores` appears in the output, including repeated scores. ```hint Look at the row after a tie In the example, the first score below the two 4.00 rows gets rank 2. Check that your numbering does the same. ``` ### Input Table(s) `Scores` has primary key `id`. | Column | Type | Meaning | |---|---|---| | id | INT | Primary key | | score | DECIMAL(5,2) | The score, with two decimal places | ### Output Contract - Columns, in this order: `score`, `rank`. - One output row per row of `Scores`. Repeated scores stay as separate rows. - `rank` is an integer. The highest score has rank 1, equal scores share a rank, and each lower distinct score has the previous rank plus 1. - `score` is returned unchanged, with its two decimal places. - Order by `score` descending, then `id` ascending. Rows with the same score are identical in the output, so the `id` tiebreak only makes the order repeatable. ### Example Input table `Scores`: | id | score | |---|---| | 1 | 3.50 | | 2 | 3.65 | | 3 | 4.00 | | 4 | 3.85 | | 5 | 4.00 | | 6 | 3.65 | Expected output: | score | rank | |---|---| | 4.00 | 1 | | 4.00 | 1 | | 3.85 | 2 | | 3.65 | 3 | | 3.65 | 3 | | 3.50 | 4 | The distinct scores from highest to lowest are 4.00, 3.85, 3.65 and 3.50, so they get ranks 1, 2, 3 and 4. Both 4.00 rows share rank 1, both 3.65 rows share rank 3, and no rank number is skipped. ### Constraints and Clarifications - `Scores` has 1 to 10,000 rows. - `score` has two decimal places, and every row has a score (no NULLs). - Two scores tie only when their stored `DECIMAL` values are equal: 3.65 ties with 3.65 and not with 3.66. - Ranks never skip a number. For scores 4.00, 4.00, 3.85 the ranks are 1, 1, 2.

Overview: Write a SQL query that ranks every score from highest to lowest, so that equal scores share a rank and the next lower score takes the next integer with no gaps. It tests window ranking functions, how ties are numbered, and ordering the final result explicitly.

NVIDIA logo
NVIDIA
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Rank the scores in Scores from highest to lowest, returning score and rank. Ties share the same rank, and the next rank after a tie is the next consecutive integer (no gaps). Order by score descending. Every row of Scores appears in the output, including repeated scores.

Input Table(s)

Scores has primary key id.

ColumnTypeMeaning
idINTPrimary key
scoreDECIMAL(5,2)The score, with two decimal places

Output Contract

  • Columns, in this order: score , rank .
  • One output row per row of Scores . Repeated scores stay as separate rows.
  • rank is an integer. The highest score has rank 1, equal scores share a rank, and each lower distinct score has the previous rank plus 1.
  • score is returned unchanged, with its two decimal places.
  • Order by score descending, then id ascending. Rows with the same score are identical in the output, so the id tiebreak only makes the order repeatable.

Example

Input table Scores:

idscore
13.50
23.65
34.00
43.85
54.00
63.65

Expected output:

scorerank
4.001
4.001
3.852
3.653
3.653
3.504

The distinct scores from highest to lowest are 4.00, 3.85, 3.65 and 3.50, so they get ranks 1, 2, 3 and 4. Both 4.00 rows share rank 1, both 3.65 rows share rank 3, and no rank number is skipped.

Constraints and Clarifications

  • Scores has 1 to 10,000 rows.
  • score has two decimal places, and every row has a score (no NULLs).
  • Two scores tie only when their stored DECIMAL values are equal: 3.65 ties with 3.65 and not with 3.66.
  • Ranks never skip a number. For scores 4.00, 4.00, 3.85 the ranks are 1, 1, 2.