π RANK() is a SQL window function that assigns a ranking to each row within a result set based on a specified ordering. Unlike ROW_NUMBER(), rows with the same ordering value receive the same rank, and the next rank is skipped, creating gaps in the ranking sequence.
π What is RANK()?
RANK() evaluates rows according to the ORDER BY clause inside the OVER() clause. Rows with equal values receive identical ranks. After a tie, the following rank is increased to account for the tied rows.
Information
π― Why Use RANK()?
RANK() is ideal when equal values should receive the same ranking.
- π Rank records based on a value.
- π Handle tied values fairly.
- π Build leaderboards.
- π Compare performance within groups.
- π Create analytical reports.
π Sample Table
| EmployeeID | EmployeeName | Department | Salary |
|---|---|---|---|
| 101 | Alice | Sales | 70000 |
| 102 | Bob | Sales | 65000 |
| 103 | Charlie | IT | 80000 |
| 104 | David | IT | 80000 |
| 105 | Emma | HR | 55000 |
π Basic Syntax
RANK() Syntax
SELECT
column1,
column2,
RANK() OVER (
ORDER BY column_name
) AS RankValue
FROM table_name;The ORDER BY clause determines how the rows are ranked.
π‘ Example: Rank Employees by Salary
Overall Salary Ranking
SELECT
EmployeeName,
Salary,
RANK() OVER (
ORDER BY Salary DESC
) AS SalaryRank
FROM Employees;π Example Output
| EmployeeName | Salary | SalaryRank |
|---|---|---|
| Charlie | 80000 | 1 |
| David | 80000 | 1 |
| Alice | 70000 | 3 |
| Bob | 65000 | 4 |
| Emma | 55000 | 5 |
Remember
π‘ Example: Rank Within Each Department
Department Ranking
SELECT
EmployeeName,
Department,
Salary,
RANK() OVER (
PARTITION BY Department
ORDER BY Salary DESC
) AS DepartmentRank
FROM Employees;PARTITION BY restarts the ranking separately for each department.
π Example Output
| Employee | Department | Salary | DepartmentRank |
|---|---|---|---|
| Alice | Sales | 70000 | 1 |
| Bob | Sales | 65000 | 2 |
| Charlie | IT | 80000 | 1 |
| David | IT | 80000 | 1 |
| Emma | HR | 55000 | 1 |
π‘ Example: Top Two Ranked Employees
Top Ranked Employees
SELECT *
FROM (
SELECT
EmployeeName,
Salary,
RANK() OVER (
ORDER BY Salary DESC
) AS SalaryRank
FROM Employees
) RankedEmployees
WHERE SalaryRank <= 2;Since tied rows share the same rank, the number of rows returned may be greater than two.
βοΈ RANK() vs ROW_NUMBER() vs DENSE_RANK()
| Function | Ties Share Rank | Gaps After Ties |
|---|---|---|
| ROW_NUMBER() | β No | Not Applicable |
| RANK() | β Yes | β Yes |
| DENSE_RANK() | β Yes | β No |
πΌ Real-World Applications
- π Sports competition rankings.
- π° Rank employees by salary.
- π Identify top-selling products.
- π Rank customers by total purchases.
- π Display student exam rankings.
ποΈ Database Compatibility
| Database System | RANK() Support |
|---|---|
| MySQL | β Supported in MySQL 8.0 and later. |
| PostgreSQL | β Fully supported. |
| SQL Server | β Fully supported. |
| Oracle | β Fully supported. |
| SQLite | β Supported in SQLite 3.25.0 and later. |
β οΈ Common Mistakes
- β Expecting consecutive rankings after tied values.
- β Confusing RANK() with ROW_NUMBER().
- β Omitting ORDER BY inside the OVER() clause.
- β Forgetting to use PARTITION BY when rankings should restart for each group.
Warning
β οΈ Best Practices
Best Practice
π Key Points to Remember
- π RANK() is a SQL window function.
- π Equal values receive the same rank.
- π Ranking gaps appear after tied values.
- π It requires the OVER() clause.
- π PARTITION BY restarts rankings within groups.
- π It is ideal for leaderboards and competition-style rankings.