Accepted Solution with subqueries and group by


  • 15
    M

    Solution with sub-queries

    We just get the rows the distinct rows that are <= that each score, count them and wrap them in an external SELECT for formatting.

    SELECT Scores.Score, Q3.Rank
    FROM(
        SELECT Q1.Score as Score, COUNT(Q1.Score) as Rank
        FROM 
            (SELECT DISTINCT Score from Scores) as Q1,
            (SELECT DISTINCT Score from Scores) as Q2
        WHERE Q1.Score <= Q2.Score
        GROUP BY Q1.Score
        ) as Q3, Scores
    WHERE Q3.Score = Scores.Score
    ORDER BY Scores.Score DESC

  • 1
    S

    AMAZING!!!
    I AN SHOCKED!!
    IT IS A WANDERFUL SOLUTION


  • 0
    C

    A brilliant way to rank the data. That's GENIUS!


  • 0
    M

    Glad it helped!


  • 0
    X

    i was shocked


Log in to reply
 

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