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.

Result:

MySQL Rank Function in Query

Anvesh Patel

Leave a Reply

2 Comments on "Calculation of Rank in MySQL Query"

Notify of
avatar
Sort by:   newest | oldest | most voted
Nick
Guest

Very interesting article !

Hain
Guest

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

wpDiscuz