1. 程式人生 > 其它 >【SQL】LeetCode-Rank Scores

【SQL】LeetCode-Rank Scores

技術標籤:SQL

LeetCode 178:Rank Scores

【Description】

Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no “holes” between ranks.

在這裡插入圖片描述
For example, given the above Scores table, your query should generate the following report (order by highest score):
在這裡插入圖片描述
Important Note: For MySQL solutions, to escape reserved words used as column names, you can use an apostrophe before and after the keyword. For example Rank.

【Solution】

select Score,
	dense_rank() over (order by Score desc) as 'Rank'
from Scores

dense_rank() means that there are two people rank first, the third one still rank second.