Calculation of Rank in MySQL Query

In this post, I demonstrate the calculation of Rank in MySQL query.

The RANK() available in Microsoft SQL Server and MySQL doesn’t have any default RANK().

You can calculate RANK using an inline query variable for checking previous value.

If a previous record matches to the current record, then increment your rank variable.The ORDER BY clause is required for that column on which you are going to calculate RANK.

Below is a full example:

Let’s First creates sample table and data.

Now let’s calculate RANK on employee salary.


MySQL Rank Function in Query

Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

If you like this post, then please share it with others.
Please follow, I will share my experience towards the success of Database Research and Development Activity.

I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.

More from

Leave a Reply

2 Comments on "Calculation of Rank in MySQL Query"

Notify of
Sort by:   newest | oldest | most voted

Very interesting article !


Thanks Anvesh, You saved my time and guys this is best blog for different database related solution.