1. Home
SQL

SQL Tutorial: Learn Structured Query Language Basics

Learn all SQL tutorial concepts in detail and master your career today.

  • 59
  • 9 Hours
right-top-arrow

Tutorial Playlist

81 Lessons
60

Mastering the RANK Function in SQL: A Comprehensive Guide

Updated on 19/07/2024485 Views

Introduction

Welcome to our handbook, on the RANK feature in SQL. In the realm of SQL, having a grasp of ranking functions is crucial for analyzing data, creating reports, and making decisions. The RANK function, along with its companions like ROW_NUMBER and DENSE_RANK enables SQL developers to efficiently rank datasets based on criteria.

In this manual, we will explore the intricacies of the RANK function delving into its structure, practical applications, and advanced concepts. Whether you are new to the subject and eager to understand the fundamentals or a seasoned professional looking to hone your expertise, this guide offers insights for all levels. Let's get started!

Overview

The RANK function in SQL is a powerful tool for sorting and ranking data within a dataset based on specified criteria. It assigns a unique ranking to each row, allowing users to identify the relative position of data points. Understanding how to utilize the RANK function effectively is crucial for various data analysis tasks, such as identifying top performers, analyzing trends, and partitioning data sets. In this comprehensive guide, we will explore the RANK function in SQL in detail, covering its syntax, usage scenarios, and advanced concepts. We will also compare it with other ranking functions like ROW_NUMBER and DENSE_RANK to understand their differences and when to use each one. Additionally, we will provide real-life examples, practical tips, and answers to frequently asked questions to help you master the RANK function and elevate your SQL skills.

What Is Rank Function In SQL

The RANK function in SQL is used to assign a rank to each row within a result set based on the specified ordering criteria. It is particularly useful for identifying the relative position of rows according to certain attributes or values. Here is an explanation of the RANK function with an example: Consider a table named ‘Sales’ with the following data:

Salesperson

SalesAmount

John

5000

Mary

7000

Adam

6000

Sarah

5500

Emily

8000

Now, let's say we want to rank these salespersons based on their sales amounts. We can use the RANK function to achieve this. The syntax of the RANK function is as follows:

Code - RANK() OVER (ORDER BY expression)

Here's how you can use the RANK function to rank the salespersons based on their sales amounts:

Code- 

SELECT 

    Salesperson,

    SalesAmount,

    RANK() OVER (ORDER BY SalesAmount DESC) AS SalesRank

FROM 

    Sales;

This query will produce the following result:

Salesperson

SalesAmount

SalesRank

Emily

8000

1

Mary

7000

2

Adam

6000

3

Sarah

5500

4

John

5000

5

Explanation:

  • The RANK function is applied to the SalesAmount column.
  • The result set is ordered by SalesAmount in descending order (highest sales amount first).
  • Each row is assigned a rank based on its sales amount.
  • In the case of ties (i.e., two or more rows with the same sales amount), the same rank is assigned to those rows, and the next rank is skipped (this is known as “rank skipping").

In the example above, Emily has the highest sales amount and is ranked first, while John has the lowest sales amount and is ranked fifth.

This is a basic example of how the RANK function works in SQL. It provides a simple yet powerful way to rank rows within a result set based on specified criteria.

Comparison between RANK(), ROW_NUMBER(), and DENSE_RANK()

  • RANK(): The RANK() function assigns a unique rank to each row within a result set based on the specified ordering criteria. It leaves gaps in the ranking sequence when there are ties. For example, if two rows have the same ranking, the next rank value will be skipped, resulting in non-consecutive ranks.
  • ROW_NUMBER(): The ROW_NUMBER() function assigns a unique sequential integer to each row within a result set, without any gaps. It does not handle ties; each row receives a distinct row number, regardless of duplicate values in the ordering column.
  • DENSE_RANK(): The DENSE_RANK() function is similar to RANK(), but it does not leave gaps in the ranking sequence. In the case of ties, it assigns the same rank to each tied row, and the next rank is not skipped. Therefore, the ranks are consecutive without any gaps.

    When to Use Each Function:
  • RANK(): Use the RANK() function to rank data with gaps in the ranking sequence. For example, if you want to rank students based on their exam scores and have the same rank for students with tied scores, the next rank should be skipped.
  • ROW_NUMBER(): Use the ROW_NUMBER() function when you need a unique sequential integer assigned to each row, especially in scenarios where you do not want gaps in the numbering sequence. For instance, when paginating results or generating unique identifiers for rows.
  • DENSE_RANK(): Use the DENSE_RANK() function when you want consecutive ranks without gaps, even in case of ties. It's suitable for scenarios where you want to ensure that each rank is assigned to a unique row and there are no skipped ranks.

Example:

Consider a table named ‘Students’ with the following data:

StudentID

Name

ExamScore

1

John

85

2

Mary

90

3

Adam

80

4

Sarah 

90

5

Emily

85

Now, let's use these functions to rank the students based on their exam scores:

Code- 

SELECT 

    StudentID,

    Name,

    ExamScore,

    RANK() OVER (ORDER BY ExamScore DESC) AS RankUsingRANK,

    ROW_NUMBER() OVER (ORDER BY ExamScore DESC) AS RankUsingROW_NUMBER,

    DENSE_RANK() OVER (ORDER BY ExamScore DESC) AS RankUsingDENSE_RANK

FROM 

    Students;

The query will produce the following result:

StudentID

Name

ExamScore

RankUsingRANK

RankUsingROW_NUMBER

RankUsingDENSE_Rank

2

Mary

90

1

1

1

4

Sarah 

90

1

2

1

1

John

85

3

3

2

5

Emily

85

3

4

2

3

Adam

80

5

5

3

Explanation:

  • Using RANK(): Both Mary and Sarah have the same exam score (90), so they are assigned the same rank (1), and the next rank (2) is skipped.
  • Using ROW_NUMBER(): Each row receives a distinct row number based on the order of exam scores, without any gaps.
  • Using DENSE_RANK(): The ranks are consecutive without any gaps, even when there are ties in exam scores.

Variations in Syntax between SQL and SQL Server:

In terms of syntax, there are no significant differences between using the RANK function in standard SQL and SQL Server. Both use the same syntax for the RANK function, as shown in the examples above. However, it's worth noting that SQL Server offers additional options for the RANK function, such as specifying a PARTITION BY clause to divide the result set into partitions before applying the ranking function. This can be useful for more advanced ranking scenarios. Example with PARTITION BY clause in SQL Server:

Code- 

SELECT 

    Salesperson,

    SalesAmount,

    RANK() OVER (PARTITION BY Region ORDER BY SalesAmount DESC) AS SalesRank

FROM 

    Sales;

This query ranks the salespersons within each region separately based on their sales amounts.In summary, while the basic syntax of the RANK function remains consistent between SQL and SQL Server, SQL Server offers additional features like the PARTITION BY clause for more advanced ranking operations.

RANK() OVER PARTITION BY SQL

1. Explanation of the Partitioning Concept:

The PARTITION BY clause in the RANK() function allows you to divide the result set into partitions based on one or more columns. Each partition is then ranked independently, meaning that the rank is reset for each partition. This can be particularly useful when you want to rank data within specific groups or categories.

For example, consider a sales dataset with salespersons and their sales amounts, where you want to rank the salespersons within each region separately. By partitioning the data by region, you can rank the salespersons independently within each region, allowing for more granular analysis.

2. Syntax and Examples of RANK() OVER PARTITION BY:

Basic Syntax:

Code- 

RANK() OVER (PARTITION BY column_name1, column_name2, ... ORDER BY expression)

Example:

Consider a table named ‘Sales’ with the following data:

Salesperson

Region

SalesAmount

John

East

5000

Mary

East

7000

Adam

West

6000

Sarah

West

5500

Emily

East

8000

Now, let's use the RANK() function with PARTITION BY to rank the salespersons within each region separately:

Code- 

SELECT 

    Salesperson,

    Region,

    SalesAmount,

    RANK() OVER (PARTITION BY Region ORDER BY SalesAmount DESC) AS SalesRankWithinRegion

FROM 

    Sales;

This query will produce the following result:

Salesperson

Region

SalesAmount

SalesRankWithinRegion

Mary

East

7000

1

Emily

East

8000

2

Adam

West

6000

1

Sarah

West

5500

2

John

East

5000

3

Explanation:

  • The result set is partitioned by the ‘Region’ column.
  • Within each partition (East and West), the salespersons are ranked based on their sales amounts in descending order.
  • Mary and Emily have the highest sales amounts within the East region and are ranked 1 and 2 respectively.
  • Adam and Sarah have the highest sales amounts within the West region and are ranked 1 and 2 respectively.
  • John has the lowest sales amount within the East region and is ranked 3.

In summary, using RANK() OVER PARTITION BY allows you to rank data within specific groups or partitions, enabling more detailed analysis and insights.

Rank Function in SQL Server is explained above in the topic Variations in Syntax between SQL and SQL Server.

The Dense Rank SQL is explained above in the topic Comparison between RANK(), ROW_NUMBER(), and DENSE_RANK().

The ‘Rank In SQL’ is explained above in the topic Comparison between RANK(), ROW_NUMBER(), and DENSE_RANK().

The Rank Query in SQL has been explained above in the topic What Is Rank Function In SQL?

Wrapping Up!

In conclusion, the RANK function in SQL is a versatile tool that allows for the ranking of data within a dataset based on specified criteria. Through this comprehensive guide, we have explored the intricacies of the RANK function, including its syntax, usage scenarios, and advanced concepts. We have also learned how the RANK function assigns a unique rank to each row, providing valuable insights into the relative position of data points. Additionally, we have compared RANK with other ranking functions like ROW_NUMBER and DENSE_RANK, understanding their differences and when to use each function.

Finally, mastering the RANK function in SQL is essential for data analysis, reporting, and decision-making tasks. Whether you are a beginner learning the basics or an experienced practitioner seeking to deepen your understanding, the RANK function is a fundamental tool in your SQL toolkit. By harnessing its power, you can unlock valuable insights from your data and make informed decisions to drive business success.

FAQs

1. What is a real-life example of RANK() and DENSE_RANK()?

A real-life example of using the RANK() and DENSE_RANK() functions in SQL can be seen in the following sales performance analysis scenario within a retail company:

Scenario: Suppose a retail company wants to analyze the sales performance of its sales representatives across different regions. The company has a database table named ‘Sales’ with the following columns: Salesperson, Region, SalesAmount.

Objective: The objective is to rank the salespersons within each region based on their sales performance and identify the top performers.

Using RANK() Function: The RANK() function can be used to assign a rank to each salesperson within each region based on their sales amount. This would allow the company to identify the relative performance of each salesperson within their respective regions.

Using DENSE_RANK() Function: On the other hand, the DENSE_RANK() function can be used if the company wants to ensure that each rank is unique within each region, even in the case of ties. This would be particularly useful if the company wants to identify the top N performers within each region without skipping ranks.

Example Output: Suppose the company runs the following SQL query:

Code- 

SELECT 

    Region,

    Salesperson,

    SalesAmount,

    RANK() OVER (PARTITION BY Region ORDER BY SalesAmount DESC) AS Rank,

    DENSE_RANK() OVER (PARTITION BY Region ORDER BY SalesAmount DESC) AS DenseRank

FROM 

    Sales;

The output of this query would show the salespersons ranked within each region based on their sales amount, along with their corresponding ranks using both RANK() and DENSE_RANK() functions.

This analysis would help the company identify top-performing salespersons within each region and make informed decisions regarding incentives, training, or resource allocation to improve sales performance.

2. How do you calculate rank?

Rank calculation involves assigning a numerical value to each item in a dataset based on specified criteria, indicating its relative position compared to others. Sorting the data based on the criteria, assigning sequential ranks, and handling ties are the key steps in calculating ranks.

3. Why do we use rank and Dense_rank?

We use rank and dense_rank to assign numerical values to items in a dataset, indicating their relative positions. Rank leaves gaps in the case of ties, while dense_rank ensures consecutive ranks even with ties, offering flexibility in data analysis and comparison.

4. Is RANK() an aggregate function?

The RANK() function is not an aggregate function. It is a window function used to assign ranks to rows within a result set based on specified ordering criteria. While it operates over a set of rows like aggregate functions, it does not perform calculations across multiple rows instead, it assigns a rank to each row based on its position within the ordered set.

The rest of the FAQs are explained above in the article, starting from the topic What Is Rank Function In SQL, sequentially.

Mukesh Kumar

Mukesh Kumar

Working with upGrad as a Senior Engineering Manager with more than 10+ years of experience in Software Development and Product Management.

Get Free Career Counselling
form image
+91
*
By clicking, I accept theT&Cand
Privacy Policy
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
right-top-arrowleft-top-arrow

upGrad Learner Support

Talk to our experts. We’re available 24/7.

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...