Advanced SQL Functions
SQL Window Functions – ROW_NUMBER vs RANK
Ranking rows with window functions
By Sharanmeet Singh••
Tags:SQLWindow FunctionsROW_NUMBERRANK
SQL Query Generator
Generate SQL queries for database operations
SQL Window Functions
Window functions let you run calculations across a set of rows related to the current row.
ROW_NUMBER Example
SELECT id, name, ROW_NUMBER() OVER (ORDER BY salary DESC) AS row_num
FROM employees;
Assigns a unique row number based on salary.
RANK Example
SELECT id, name, RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees;
Gives the same rank for ties.
Key Difference
ROW_NUMBER
→ No duplicatesRANK
→ Handles ties by skipping numbers
Tip: Use
DENSE_RANK
if you don’t want gaps in ranking.