Approach: Using Inner Join


  • 0
    P

    Overview:

    First Step: We will first pull distinct score from Scores tables, then will rank them in decreasing order. /* this will be our first table for the inner join*/
    Second Step: We will do inner join between our first table and the base table on score value.

    MYSQL Query:

    SELECT
    c.score as Score,
    CAST(d.rank as signed) as Rank
    from
    Scores c,
    (
    SELECT score, @rank:=@rank+1 AS rank from (select distinct score from Scores order by score desc) as b, (select @rank:=0) as a
    ) d
    where c.score = d.score
    order by c.score desc
    ;

     NOTE: Subquery used is for ranking the distinct score value in 
     decreasing order.

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.