Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2015 August Calculation of Rank in MySQL Query

Calculation of Rank in MySQL Query

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing a demonstration on how to calculate the 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 the previous value.

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

Below is a full example:

Let’s First creates sample table and data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
CREATE DATABASE Employee;
 
CREATE TABLE Employee.tbl_EmpSalary
(
EmpID INT
,EmpSalary INT
);
INSERT INTO Employee.tbl_EmpSalary VALUES
(1, 10000)
,(2, 20000)
,(3, 26000)
,(4, 10000)
,(5, 35000)
,(6, 40000)
,(7, 42000)
,(8, 20000);

Now let’s calculate RANK on employee salary.

1
2
3
4
5
6
7
8
9
10
11
12
13
SET @PreviousRecord = NULL;
SET @Rank = 0;
SELECT
EmpID
,EmpSalary
,CASE
WHEN @PreviousRecord = EmpSalary
THEN @Rank
WHEN @PreviousRecord := EmpSalary
THEN @Rank := @Rank + 1
END AS EmpSalaryRank
FROM Employee.tbl_EmpSalary
ORDER BY EmpSalary;

Result:

MySQL Rank Function in Query

Aug 29, 2015Anvesh Patel
Prepared or Parameterized Statements in Database SystemArrange Rows to Columns Pivot Table in MySQL
Comments: 9
  1. Nick
    September 4, 2015 at 5:14 pm

    Very interesting article !

  2. Hain
    September 24, 2015 at 2:08 am

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

  3. babon
    April 17, 2018 at 11:46 am

    my phpadmin doesnt like this code…’x’ everywhere after ‘CASE’ is used.

    • Anvesh Patel
      Anvesh Patel
      April 18, 2018 at 9:13 pm

      Please share more detail on this.

  4. Charan
    June 19, 2018 at 5:58 pm

    Thanks for sharing; I am trying to create a composite rank based on four variables / limits; So that I can distribute/ allocate (1) twenty workstations ( Maxium Rank Limit) to over (2) one hundred teachers based on their (3) time-table on (4) weekdays (M-F) so that no two persons with same time table on a given day/time get alloted same workstation -;) Please advice, if this is possible using a MySQL RANK query. Each teacher has a maxium of ten enteries.

    • Anvesh Patel
      Anvesh Patel
      June 19, 2018 at 6:52 pm

      Yes possible, group all your elements and then calculate rank and then select only top 10 entries for each teacher.

  5. Charan
    June 20, 2018 at 12:15 pm

    Thanks Anvesh. Could you please demonstrate -MySQL query- how could Composite Rank be computed, using three or four variables?

    • Anvesh Patel
      Anvesh Patel
      June 20, 2018 at 5:49 pm

      Share your query/data detail via Contact Me page, I will guide you.

  6. Charan
    June 27, 2018 at 6:41 pm

    Just taking a followup, in case some solution emerged. Thanks

Anvesh Patel
Anvesh Patel

Database Engineer

August 29, 2015 MySQLAnvesh Patel, database, database research and development, MySQL, MySQL Command, MySQL Database Administrator, MySQL Database Designing, MySQL Database Programming, MySQL Error, MySQL Performance Tunning, MySQL Query, MySQL Tips and Tricks, RANK
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....