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.

Table `scores` has one row per score: `id` (INTEGER, the primary key) and `score` (DECIMAL(5,2), two decimal places, never NULL). The table has 1 to 10,000 rows. 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, so no rank number is skipped. Every row of `scores` appears in the output, including repeated scores, which stay as separate rows. 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. For scores 4.00, 4.00, 3.85 the ranks are 1, 1, 2. - `score` is returned unchanged, with its two decimal places. - Two scores tie only when their stored DECIMAL values are equal: 3.65 ties with 3.65 and not with 3.66. - 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.

Tables

scores(id INTEGER, score DECIMAL(5,2))

Hints

  1. 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.
  2. Every row of `scores` appears in the output, so a score stored on three rows shows up three times, each time with the same rank.
NVIDIAData ScientistmediumData Manipulation (SQL/Python)

Rank Scores Without Gaps

Table scores has one row per score: id (INTEGER, the primary key) and score (DECIMAL(5,2), two decimal places, never NULL). The table has 1 to 10,000 rows.

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, so no rank number is skipped. Every row of scores appears in the output, including repeated scores, which stay as separate rows.

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. For scores 4.00, 4.00, 3.85 the ranks are 1, 1, 2.
  • score is returned unchanged, with its two decimal places.
  • Two scores tie only when their stored DECIMAL values are equal: 3.65 ties with 3.65 and not with 3.66.
  • 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.

Expected Output

Tables

scores
scores
ColumnType
idINTEGER
scoreDECIMAL(5,2)

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions