![]() |
VOOZH | about |
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.
Example: First, we will create a demo SQL table to demonstrate the RANK() function
Query:
SELECT
Name,
Department,
Salary,
RANK() OVER (ORDER BY Salary DESC) AS Rank
FROM Employees;Output:
👁 Screenshot-2026-01-19-110636Syntax:
RANK() OVER (
[PARTITION BY expression, . . . ]
ORDER BY expression (ASC | DESC) );Example: We will create a Student table, which stores student data including roll_no, name, age, class, and marks.
Query:
SELECT
StudentID,
Name,
Marks,
RANK() OVER (ORDER BY Marks DESC) AS Rank
FROM students;Output:
👁 Screenshot-2026-01-15-164226RANK() function gives each student a rank based on Marks in descending order.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 order | Assigns rank like RANK() but without gaps | Assigns a unique sequential number to each row |
| Same rank for tied values | Same rank for tied values | No tie handling; always unique |
| Skips rank after tie | No skipping | Consecutive numbers |