RANK() Function in SQL Server

Last Updated : 19 Jan, 2026

SQL RANK() is used to assign a rank or position to each row within a result set based on a specified order. Rows with the same values receive the same rank, and the next rank is skipped for ties.

  • PARTITION BY: Ranks are assigned independently within each group defined by PARTITION BY.
  • ORDER BY: Determines the order of ranking within the result set or within each partition.

Example: First, we will create a demo SQL table to demonstrate the RANK() function

Screenshot-2026-01-19-110549
Employees Table

Query:

SELECT 
    Name,
    Department,
    Salary,
    RANK() OVER (ORDER BY Salary DESC) AS Rank
FROM Employees;

Output:

Screenshot-2026-01-19-110636

Syntax:

RANK() OVER (
   [PARTITION BY expression, . . . ]
   ORDER BY expression (ASC | DESC) );
  • RANK(): Assigns a rank or position to each row based on the specified order.
  • PARTITION BY (optional): Divides the result set into groups and ranks rows independently within each group.
  • ORDER BY: Specifies the order for ranking in ascending (ASC) or descending (DESC) order.

Example: We will create a Student table, which stores student data including roll_no, name, age, class, and marks.

Screenshot-2026-01-15-164103
Student Table

Query:

SELECT
    StudentID,
    Name,
    Marks,
    RANK() OVER (ORDER BY Marks DESC) AS Rank
FROM students;

Output:

Screenshot-2026-01-15-164226
  • The RANK() function gives each student a rank based on Marks in descending order.
  • Students with the same marks get the same rank.
  • The next rank is skipped after a tie.

RANK() vs DENSE_RANK() vs ROW_NUMBER()

Below are the comparison of the three common ranking functions in SQL Server: RANK(), DENSE_RANK(), and ROW_NUMBER().

RANK()DENSE_RANK()ROW_NUMBER()
Assigns rank based on orderAssigns rank like RANK() but without gapsAssigns a unique sequential number to each row
Same rank for tied valuesSame rank for tied valuesNo tie handling; always unique
Skips rank after tieNo skippingConsecutive numbers
Comment