mysqlwindow-functionsdense-rank

Why can't I use dense_rank for SQL 'rank scores'?


I'm using the dense_rank function in SQL to solve the leetcode 'rank scores' problem(https://leetcode.com/problems/rank-scores/description/):

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

It always give me the following error:

Line 2: SyntaxError: near '(order by Score) Rank
from Scores
order by Score desc'

I wonder how to make this answer correct? Thanks a lot!

Also, I realized most people use an answer without using the DENSE_RANK function, which is quite confusing since to me DENSE_RANK is probably the most intuitive way to solve the problem. Anyone have any idea? Thanks again!


Solution

  • Edited answer

    My SQL does not support dense_rank, it does support other window functions though. Check out this answer for help.

    Alternatively you could run the code in another SQL server