Rank the scores
Mediumsqlwindow functionsdense rank
Return score and rank for every row in results. The highest gets rank 1, equal scores share a rank, and there are no gaps (two 1s are followed by 2). Sort from highest, then by id.
Tables
- results (id INTEGER, score INTEGER)
Example 1
results
| id | score |
|---|---|
| 1 | 350 |
| 2 | 365 |
| 3 | 400 |
| 4 | 385 |
| 5 | 400 |
| 6 | 365 |
Expected result
| score | rank |
|---|---|
| 400 | 1 |
| 400 | 1 |
| 385 | 2 |
| 365 | 3 |
| 365 | 3 |
| 350 | 4 |
Constraints
- 0 ≤ rows ≤ 1000
- The order of rows matters
Submit checks your solution against 7 tests, including hidden ones.
SQL
-- Write one SELECT query. Tables: results SELECT