RANK() in SQL

πŸ† 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

RANK() is commonly used for leaderboards, competition rankings, top-performing employees, and analytical reports where ties should share the same position.

🎯 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

EmployeeIDEmployeeNameDepartmentSalary
101AliceSales70000
102BobSales65000
103CharlieIT80000
104DavidIT80000
105EmmaHR55000

πŸ“ 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

EmployeeNameSalarySalaryRank
Charlie800001
David800001
Alice700003
Bob650004
Emma550005

Remember

Notice that both Charlie and David receive Rank 1. The next employee receives Rank 3, not Rank 2, because RANK() leaves gaps after ties.

πŸ’‘ 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

EmployeeDepartmentSalaryDepartmentRank
AliceSales700001
BobSales650002
CharlieIT800001
DavidIT800001
EmmaHR550001

πŸ’‘ 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()

FunctionTies Share RankGaps After Ties
ROW_NUMBER()❌ NoNot 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 SystemRANK() 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

When multiple rows have identical ordering values, RANK()assigns the same rank to all tied rows and skips subsequent rank numbers. If you need consecutive rankings without gaps, use DENSE_RANK()instead.

⚠️ Best Practices

Best Practice

Use RANK() when ties should receive the same position, always specify a deterministic ORDER BY, use PARTITION BY for group-based rankings, and choose ROW_NUMBER() or DENSE_RANK() if their ranking behavior better matches your business requirements.

πŸš€ 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.
>>"RANK() rewards ties fairlyβ€”even if it leaves a gap for the next competitor."

Summary

βœ… RANK() is a powerful SQL window function that assigns rankings while allowing tied rows to share the same position. Because it skips rank numbers after ties, it is especially useful for competition rankings, leaderboards, and analytical reports where equal values deserve equal ranks.