MySQL RANK

Learn via video courses
Topics Covered

Overview

MySQL rank() function assigns the ranking number to the rows of the result set or the partition and ranking numbers are skipped by the MySQL rank() function if there are peers. Apart from that, we can also use the MySQL rank() function with partitions for assigning the ranks to the partition rows. The rank starts from 1 for the new partition. MySQL version 8.0 and onwards version support the rank() function.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax of MySQL RANK() Function

Parameters of MySQL RANK() Function

  • Partition by Clause:
    For partitioning the result set into different groups we use Partition by clause and which is an optional clause in the MySQL rank() function. If partition by clause is not provided then the whole result is treated as one partition.
  • Order by Clause:
    Order by clause is used for specifying the order of assigning the rank number. And this is also an optional clause. asc|desc can be written with the order by clause for ascending and descending order of the ranking, and if have not specified asc|desc then by default it will use ascending order.
Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

Return Value of MySQL RANK() Function

The rank assigned to every row is returned by the MySQL rank() function.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Exceptions of MySQL RANK() Function

  • If the rank() function is used with the query that has LIMIT clause, then the rank assignment for the rows excluded from the limit may be inaccurate.
  • Rank is assigned according to the defined groups if the rank() function is used with the query that has GROUP BY clause.

How Does the MySQL RANK() Function Work?

MySQL RANK() function assigns the ranking number to the rows of the partition or results set based on the ordering specified by the order by clause. It will not assign consecutive rankings to the rows. If duplicate rows exist then it assigns the same ranking to the duplicate rows and the row after the duplicate row will get the ranking as the sum of the previous rank and the number of duplicate rows. For instance, if the first three rows have the same value, then 1 is assigned by the rank function to all three rows, and 3 is assigned to the fourth row.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Examples

Creating a Table:

Let us create a table with the name STUDENT and we will store the STUDENT_NAME, STUDENT_CLASS, and STUDENT_PERCENTAGE in the table. So the code for creating the STUDENT table is given below:

Query:

Inserting Data Into the Table:

Now let us insert some data into the table.

Query:

Fetching Data of The Table:

Now first we all fetch all data of the table by writing the query given below:

Query:

Output:

Example1: Using Mysql Rank() Function:

Now we will rank() function to get the data of the students with the rank assigned to them based on their percentage.

Query:

Output:

In the above query, we have not specified the asc|desc with the order by clause so it has automatically arranged the student_percentage into ascending order.

Example2: Using Desc with The Order by Clause in My Sql Rank() Function

If we want ranking will be assigned in the decreasing order of the STUDENT_PERCENTAGE, then we need to write the query given below:

Query:

Output:

Example3: Using Partition by Clause with My Sql Rank() Function

We can also assign a rank to the students class-wise. Then we have to write partition by clause with the STUDENT_CLASS column so that the class-wise rank is assigned to the students based on their percentage.

Query:

Output:

Conclusion

  • MySQL rank() function assigns the ranking number to the rows of the result set or the partition.
  • For partitioning the result set into different groups we use Partition by clause and which is an optional clause in the MySQL rank() function.
  • Order by clause is also an optional clause used for specifying the order of assigning the rank number.
  • If duplicate rows exist then it assigns the same ranking to the duplicate rows and the row after the duplicate row will get the ranking as the sum of the previous rank and number of duplicate rows.

See Also