Rank and Dense Rank in SQL Server
Last Updated :
11 Apr, 2025
In the world of SQL, the ability to rank data based on specific criteria is important for a wide range of analytical tasks. SQL Server offers powerful functions like RANK() and DENSE_RANK(), which allow users to assign ranks to rows in a dataset. These functions are particularly useful for tasks such as determining the top performers, handling ties in results, and performing advanced data analysis.
But what is the difference between RANK() and DENSE_RANK()? In this article, we will break down the differences between these two ranking functions, explain their usage with practical examples, and explore the real-world applications of each.
Why Ranking is Important in SQL?
Before diving into the details of RANK() and DENSE_RANK(), it's essential to understand why ranking is necessary in SQL:
- Ordering Results: Ranking allows you to sort the results of a query based on specific criteria. This is fundamental for presenting data in a meaningful and organized manner, making it easier for users to interpret and analyze.
- Top-N Queries: Ranking is indispensable for identifying the highest or lowest N records in a dataset according to specific conditions. This is valuable in scenarios where you need to focus on the most significant or extreme values, such as identifying top-performing products or addressing quality control issues.
- Analytical Functions: Ranking is a cornerstone of various advanced analytical functions. It serves as a foundational step for calculating percentiles, quartiles, medians, and other statistical measures. These functions provide deeper insights into the distribution and characteristics of the data.
- Handling Ties: When you have tied values (i.e., multiple records with the same value), ranking ensures that each record receives an appropriate rank. This is crucial for maintaining accuracy and fairness in scenarios where tied values must be differentiated, such as in sports competitions or financial rankings.
- Pagination: In web applications and other software, ranking is essential for implementing pagination. It allows for the efficient display of large datasets by breaking them into manageable chunks. Users can navigate through pages of results, enhancing the user experience.
1. RANK() Function
The RANK() function in SQL Server is a tool used to assign a position or rank to each row in a result set based on specific criteria. If two or more rows share the same values according to the specified criteria, they will receive the same rank. In this scenario, the rank of the next row is incremented by the number of tied rows.
For example, if two rows tie for the first position, the next row will be assigned a rank of 3, not 2. This function is particularly valuable when you need to determine the relative position of rows in a sorted dataset. It's extensively utilized in analytical queries to gain insights and perform calculations based on these rankings.
Syntax:
RANK() OVER (PARTITION BY column1, column2, ... ORDER BY sort_column1, sort_column2, ...)
Parameters
- RANK(): The function itself.
- PARTITION BY: An optional clause that divides the result set into partitions or groups. The ranking is applied within each partition separately. If omitted, the entire result set is treated as a single partition.
- ORDER BY: This clause specifies the columns by which the data is sorted to determine the ranking order.
Example
Let's assume we have a table called 'Students' with the following data. In this table, Alice has a score of 90.
Name | Score |
---|
John | 85 |
Jane | 90 |
Mark | 85 |
Alice | 90 |
Query:
SELECT Name, Score,
RANK() OVER (ORDER BY Score DESC) AS Rank
FROM Students;
Output:

Explanation:
- Both Jane and Alice have the highest score, so they share the top rank (1), and the next rank is 3.
- John and Mark both have the same score (85), so they share rank 3. The next rank is 5.
2. DENSE_RANK() Function
The DENSE_RANK() function in SQL server serves the purpose of assigning ranks to rows in a dataset according to specific conditions. Just like the RANK() function, it orders the data based on certain criteria. However, what sets it apart is that it ensures there are no gaps between ranks in cases where multiple rows share the same values. This means that tied rows receive consecutive ranks without any interruptions.
DENSE_RANK() is particularly useful when we need a continuous and unbroken sequence of ranks. It's especially valuable in situations where you require a clear and uninterrupted ordering of data, particularly when dealing with tied values. This function is widely employed in scenarios where a seamless and sequential arrangement of data is essential for accurate analysis and reporting.
Syntax:
DENSE_RANK() OVER (PARTITION BY column1, column2, ... ORDER BY sort_column1, sort_column2, ...)
Parameters
- DENSE_RANK(): The function itself.
- PARTITION BY: This optional clause divides the result set into partitions or groups, and the ranking is applied within each partition separately. If omitted, the entire result set is treated as a single partition.
- ORDER BY: This clause specifies the columns by which the data is sorted to determine the ranking order.
Example
Let's assume we have same table called 'Students' with the following data. In this table, Alice has a score of 90.
Name | Score |
---|
John | 85 |
Jane | 90 |
Mark | 85 |
Alice | 90 |
Query:
SELECT Name, Score,
DENSE_RANK() OVER (ORDER BY Score DESC) AS Dense_Rank
FROM Students;
Output:

Explanation:
- Both Alice and Jane have the highest score (90), so they share the top dense rank (1).
- John and Mark both have the same score (85), so they share dense rank 2. The next dense rank is 3.
Key Differences Between RANK() and DENSE_RANK()
Criteria | RANK() | DENSE_RANK() |
---|
Definition | Assigns a unique rank to each row, leaving gaps between ranks when there are ties. | Assigns a unique rank to each row, with no gaps in ranks, even when there are ties. |
Behavior with Ties | Skips the next rank(s) after a tie. For example, if two rows share rank 1, the next rank will be 3. | Does not skip any ranks after a tie. If two rows share rank 1, the next rank will be 2. |
Example (Without Ties) | Data: 10, 20, 30, 40 Rank: 1, 2, 3, 4 | Data: 10, 20, 30, 40 Dense Rank: 1, 2, 3, 4 |
Example (With Ties) | Data: 10, 20, 20, 30, 40 Rank: 1, 2, 2, 4, 5 | Data: 10, 20, 20, 30, 40 Dense Rank: 1, 2, 2, 3, 4 |
Gaps Between Ranks | Leaves gaps in the rank sequence after ties. For example, after a tie in rank 1, rank 2 is skipped. | No gaps in the rank sequence. Tied rows share the same rank, and the next rank is consecutive. |
Effect of Skipping Ranks | Leads to non-sequential ranks when ties occur. | Maintains sequential ranking without skipping any numbers, even with ties. |
Application | Useful when you need to differentiate between tied values distinctly (e.g., in a competition). | Ideal when you need to maintain a continuous, sequential rank without interruptions. |
Syntax | RANK() OVER (PARTITION BY ... ORDER BY ...) | DENSE_RANK() OVER (PARTITION BY ... ORDER BY ...) |
Syntax | RANK() OVER (PARTITION BY ... ORDER BY ...) | DENSE_RANK() OVER (PARTITION BY ... ORDER BY ...) |
When to Use RANK() and DENSE_RANK()?
- Use RANK() when you need to differentiate between ranks clearly, especially in cases where it's important to leave gaps between rankings when ties occur. For example, in sports competitions where ranking gaps can represent a significant difference in performance.
- Use DENSE_RANK() when you want to assign consecutive ranks, even if there are ties. This is ideal for situations like sales ranking where you don’t want to skip ranks, ensuring a seamless progression.
Real-World Use Cases of RANK() and DENSE_RANK()
- Sales Performance: Ranking salespeople based on their total sales, where ties can occur. RANK() is useful if you want to show the gap between performers, while DENSE_RANK() ensures the next highest performer doesn't miss a rank.
- Employee Rankings: When assessing employee performance metrics such as ratings or KPIs, these functions can help identify the top performers and assign ranks accordingly.
- Data Analysis: For analyzing large datasets, such as customer or product performance, ranking functions help segment data based on performance thresholds, allowing for more targeted insights.
Conclusion
In SQL Server, RANK() and DENSE_RANK() are powerful window functions for assigning ranks to rows based on specific criteria. While RANK() assigns ranks but leaves gaps in the sequence when ties occur, DENSE_RANK() ensures a continuous sequence without gaps. Understanding the differences between these two functions allows for precise ranking strategies tailored to the specific needs of the dataset at hand. Whether we’re analyzing performance, sorting datasets, or generating top-N queries, these ranking functions are indispensable tools for any SQL user.
Similar Reads
RANK() Function in SQL Server The RANK() function is a powerful window function in SQL Server used to assign a rank to each row within a result set. It is particularly useful when we need to assign a rank to a group of rows based on some sorting criteria and want to differentiate between rows that have the same values. Unlike ot
5 min read
Delete Duplicate Rows in MS SQL Server In MS SQL Server, managing duplicate rows is a common task that can affect the integrity and performance of a database. To address this issue, SQL Server provides several methods for identifying and deleting duplicate rows. In this article, We will explore three effective approaches: using the GROUP
5 min read
How to Limit Rows in a SQL Server? To limit rows in SQL Server, use the TOP clause in the SELECT statement. Using the TOP clause in SQL Server, users can limit the number of rows in the results set. Here, we will understand how to limit rows in SQL Server with the help of different examples. Steps to Limit Rows in SQL ServerLet's che
3 min read
GROUPING ID Function in SQL Server SQL Server is a Relational Database Management System that is used to create and manipulate the database. It provides advanced security measures like encryption, access control, and auditing to protect sensitive data from unauthorized access. It Supports a wide range of data types, including structu
6 min read
How to Index Foreign Key Columns in SQL Server Indexing foreign key columns in SQL Server is a vital optimization strategy for enhancing query performance, particularly when dealing with JOIN operations between related tables. Foreign keys enforce relationships between tables by linking a column (or columns) in one table to the primary key of an
4 min read
How to Return Random Rows Efficiently in SQL Server? In this article, we are going to learn an SQL Query to return random rows efficiently. To execute the query, we are going to first create a table and add data into it. We will then sort the data according to randomly created IDs(using the NEWID() method) and return the top rows after the sorting ope
2 min read