What are Window Functions?
What are Window Functions?
Window functions compute aggregated values across related partitions of rows while retaining the original row-level granularity (they do not collapse rows like GROUP BY).
Key Syntax Structure
SELECT
name, department, salary,
AVG(salary) OVER(PARTITION BY department) AS dept_avg,
DENSE_RANK() OVER(PARTITION BY department ORDER BY salary DESC) AS rank
FROM employees;
Ranking Functions Comparison
| Salary | ROW_NUMBER | RANK | DENSE_RANK | Notes |
|---|---|---|---|---|
| 100,000 | 1 | 1 | 1 | Top earner |
| 90,000 | 2 | 2 | 2 | Tied value |
| 90,000 | 3 | 2 | 2 | Tied value |
| 80,000 | 4 | 4 | 3 | RANK skips to 4; DENSE_RANK continues to 3 |