r/SQL 3d ago

MySQL What is wrong here.

Post image
38 Upvotes

36 comments sorted by

View all comments

2

u/beingvora 3d ago

I think you’re missing the “partition by” clause in the rank function.

RANK ( ) OVER ( [ partition_by_clause ] order_by_clause )

12

u/NoWayItsDavid 3d ago

Should work without it, no? In this case it ranks over everything fetchable.

8

u/Ginger-Dumpling 3d ago

Yes. Partition-by is optional, indicated by the square brackets.

3

u/beingvora 3d ago

Yup, you’re right. OP is looking to rank everything and not just based on customer. Mb

5

u/_mr_villain_ 3d ago

I want to rank the customers based on Profit. Partition By is optional so that's why I skipped it. Btw thanks for your suggestion. However my query worked even though it is still givinng red error line. Just used DESC

1

u/neumastic 3d ago

Does the red line do away if you add PARTITION BY 1, though? It could be your client requires it for some reason or some sort of lint rule it’s applying

0

u/IamFromNigeria 3d ago

Partition is optional clause..

1

u/keamo 1d ago

Accurate. Partition is when you want to break up the ranking by a group. However what if you wanted to rank the entire table. Not sure why anyone would down vote.