CathyQian
CathyQian

Reputation: 1159

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!

Upvotes: 2

Views: 3605

Answers (3)

Mueez Khan
Mueez Khan

Reputation: 1

select score ,
dense_rank() over  ( order by score desc ) as "rank"
from Scores

Use this code it will work we need to write rank as "rank" because rank is a funcion that's why it's giving error

Upvotes: -2

Ankur Ranjan
Ankur Ranjan

Reputation: 11

Windows function like rank(), dense_rank() are supported in newer version of MySQL.

Change your SQL query i.e. as "Rank" from as Rank for passing leetcode testcases.

Upvotes: 1

Raunak Thomas
Raunak Thomas

Reputation: 1403

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

Upvotes: 1

Related Questions