Rank Scores

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.

Example

Table: Score

 Id   Score 
 1    3.40  
 2    3.55  
 3    5.00  
 4    3.75  
 5    4.00  
 6    3.55  

Output:

 Output :
 score  Rank    
 5.00   1 
 4.00   2       
 3.75   3
 3.55   4       
 3.55   4       
 3.40   5


Approach

Mysql

SELECT 
    Score as Score,
    DENSE_RANK() OVER(ORDER BY Score DESCas 'Rank'
FROM 
    Scores;

Oracle 

SELECT 
    Score as Score,
    DENSE_RANK() OVER(ORDER BY Score DESCas Rank
FROM 
    Scores;


No comments:

Post a Comment