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.
CREATE DATABASE Employee;
CREATE TABLE Employee.tbl_EmpSalary
INSERT INTO Employee.tbl_EmpSalary VALUES
Now let’s calculate RANK on employee salary.
SET @PreviousRecord = NULL;
SET @Rank = 0;
WHEN @PreviousRecord = EmpSalary
WHEN @PreviousRecord := EmpSalary
THEN @Rank := @Rank + 1
END AS EmpSalaryRank
ORDER BY EmpSalary;
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 dbrnd.com, 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.