Window Functions — ROW_NUMBER, RANK & DENSE_RANK
Assign sequential numbers and competitive rankings to rows without collapsing your data into groups.
Window functions see every row AND do calculations across them. ROW_NUMBER gives unique numbers; RANK and DENSE_RANK handle ties differently.
EXERCISE
1Window Functions — Calculations Without Collapsing Rows
Window functions perform calculations across a set of rows related to the current row — without GROUP BY's side effect of collapsing everything into one row per group.
The key insight: GROUP BY collapses rows. Window functions keep all rows.
-- GROUP BY — 3 employees in IT → 1 summary row:
SELECT department, AVG(salary) AS avg_sal
FROM employees
GROUP BY department;
-- Output: IT | 77500 (one row per department)
-- Window function — 3 employees in IT → still 3 rows, but each row shows the avg:
SELECT name, department, salary,
AVG(salary) OVER (PARTITION BY department) AS dept_avg
FROM employees;
-- Output:
-- Ada | IT | 75000 | 77500
-- Alan | IT | 80000 | 77500
-- Grace | HR | 60000 | 60000
The OVER() clause is what makes it a window function:
-- OVER() → window = all rows in the result
-- OVER(PARTITION BY dept) → window = rows in the same department
-- OVER(ORDER BY salary DESC) → window = rows ordered by salary
-- OVER(PARTITION BY dept ORDER BY salary DESC) → both
ROW_NUMBER() — unique sequential number:
SELECT name, department, salary,
ROW_NUMBER() OVER (ORDER BY salary DESC) AS overall_rank,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS dept_rank
FROM employees;
-- overall_rank: ranks across all employees
-- dept_rank: ranks within each department separately
Output:
+----------+------------+--------+--------------+-----------+
| name | department | salary | overall_rank | dept_rank |
+----------+------------+--------+--------------+-----------+
| Alan | IT | 80000 | 1 | 1 |
| Ada | IT | 75000 | 2 | 2 |
| Grace | HR | 60000 | 3 | 1 |
+----------+------------+--------+--------------+-----------+
Pro Tip: Window functions were added to SQL precisely because analysts kept needing "the department average next to each individual row". They are the most powerful feature in modern SQL.
EXERCISE
2RANK vs DENSE_RANK vs ROW_NUMBER — Handling Ties
When rows have equal values, three ranking functions treat ties differently. Knowing which to use depends on whether you want gaps in ranking.
-- Sample data: salaries 80000, 75000, 75000, 60000
SELECT name, salary,
ROW_NUMBER() OVER (ORDER BY salary DESC) AS row_num,
RANK() OVER (ORDER BY salary DESC) AS rnk,
DENSE_RANK() OVER (ORDER BY salary DESC) AS dense_rnk
FROM employees;
Output:
+----------+--------+---------+-----+-----------+
| name | salary | row_num | rnk | dense_rnk |
+----------+--------+---------+-----+-----------+
| Alan | 80000 | 1 | 1 | 1 |
| Ada | 75000 | 2 | 2 | 2 |
| Priya | 75000 | 3 | 2 | 2 | ← tie
| Grace | 60000 | 4 | 4 | 3 |
+----------+--------+---------+-----+-----------+
ROW_NUMBER() — always unique, ties get arbitrary order:
- 1, 2, 3, 4 — no gaps, no ties
RANK() — ties get same rank, next rank is skipped:
- 1, 2, 2, 4 — gap at 3
DENSE_RANK() — ties get same rank, no skips:
- 1, 2, 2, 3 — no gap
When to use which:
| Use case | Function |
|---|---|
| Pagination (need unique row numbers) | ROW_NUMBER() |
| Competition ranking (ties share rank, then gap) | RANK() |
| Class ranking (ties share rank, no gap) | DENSE_RANK() |
| Finding Nth highest salary | DENSE_RANK() |
Finding the top earner per department:
-- Get rank 1 per department:
WITH ranked AS (
SELECT name, department, salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn
FROM employees
)
SELECT name, department, salary
FROM ranked
WHERE rn = 1;
Pro Tip: For "find the highest in each group" queries, ROW_NUMBER() is perfect — it always gives exactly one row per group with rank 1. RANK() and DENSE_RANK() would return multiple rows if there are ties.
EXERCISE
3NTILE & PERCENT_RANK — Dividing Data Into Groups
NTILE splits rows into equal-sized buckets. PERCENT_RANK shows where each row falls in the percentage distribution from 0 to 1.
NTILE(n) — divide rows into n equal groups:
-- Split employees into 4 salary quartiles:
SELECT name, salary,
NTILE(4) OVER (ORDER BY salary) AS quartile
FROM employees;
Output (12 employees → 3 per quartile):
+----------+--------+----------+
| name | salary | quartile |
+----------+--------+----------+
| Grace | 45000 | 1 | ← bottom 25%
| Meera | 50000 | 1 |
| Rituraj | 52000 | 1 |
| Rohan | 58000 | 2 | ← 25-50th percentile
...
| Alan | 85000 | 4 | ← top 25%
+----------+--------+----------+
Practical NTILE use case — performance tiers:
SELECT
salesperson_name,
total_sales,
NTILE(3) OVER (ORDER BY total_sales DESC) AS performance_tier,
CASE NTILE(3) OVER (ORDER BY total_sales DESC)
WHEN 1 THEN 'Top Performer'
WHEN 2 THEN 'Meets Expectations'
WHEN 3 THEN 'Needs Improvement'
END AS tier_label
FROM sales_summary;
PERCENT_RANK() — position as percentage from 0 to 1:
SELECT name, salary,
ROUND(PERCENT_RANK() OVER (ORDER BY salary) * 100, 1) AS percentile
FROM employees;
-- The lowest earner: 0.0 percentile
-- The highest earner: 100.0 percentile
CUME_DIST() — cumulative distribution:
SELECT name, salary,
ROUND(CUME_DIST() OVER (ORDER BY salary) * 100, 1) AS pct_at_or_below
FROM employees;
-- "X% of employees earn this salary or less"
Pro Tip: NTILE is great for creating tiers and buckets in dashboards. PERCENT_RANK and CUME_DIST are used in statistics and data science workflows embedded in SQL.
