SQL FOUNDATIONS:Window Functions

Use window functions for ranking, running totals, and comparisons.

SQL Window Functions Explained with Examples + Practice Questions

What are Window Functions

Window functions perform calculations across a set of rows without grouping them into a single row.

Unlike GROUP BY, they:

  • Keep all rows
  • Add computed values alongside each row

When to Use Window Functions

  • Ranking (top employees, leaderboards)
  • Running totals
  • Comparing rows (previous/next)
  • Interview problems (very common for mid–senior roles)

Example Dataset

Table: employees

idnamedepartmentsalary
1JohnHR40000
2AliceIT60000
3BobIT55000
4EmmaFinance70000
5DavidHR45000
6AnkitIT52000

Basic Syntax

sql
FUNCTION() OVER (
PARTITION BY column
ORDER BY column
)
  • OVER defines window
  • PARTITION BY groups rows (like GROUP BY but keeps rows)
  • ORDER BY defines order inside partition

ROW_NUMBER()

Assigns unique row number to each row.

sql
SELECT name, salary,
ROW_NUMBER() OVER (ORDER BY salary DESC) AS rank
FROM employees;

RANK()

Assigns rank (with gaps for ties).

sql
SELECT name, salary,
RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees;

DENSE_RANK()

Assigns rank without gaps.

sql
SELECT name, salary,
DENSE_RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees;

PARTITION BY Example

Rank employees within each department:

sql
SELECT name, department, salary,
ROW_NUMBER() OVER (
PARTITION BY department
ORDER BY salary DESC
) AS dept_rank
FROM employees;

SUM() with Window (Running Total)

sql
SELECT name, salary,
SUM(salary) OVER (ORDER BY id) AS running_total
FROM employees;

AVG() with Window

sql
SELECT name, salary,
AVG(salary) OVER () AS avg_salary
FROM employees;

LAG() (Previous Row)

sql
SELECT name, salary,
LAG(salary) OVER (ORDER BY salary) AS previous_salary
FROM employees;

LEAD() (Next Row)

sql
SELECT name, salary,
LEAD(salary) OVER (ORDER BY salary) AS next_salary
FROM employees;

Key Concepts

  • Window functions do not collapse rows
  • PARTITION BY groups logically
  • ORDER BY defines sequence
  • Useful for ranking and comparisons

Window Functions vs GROUP BY

FeatureWindow FunctionsGROUP BY
OutputKeeps all rowsReduces rows
Use caseRanking, running totalsAggregation
FlexibilityHighLimited

Common Questions (FAQ)

What is a window function

A function that performs calculations across rows without grouping them.

Difference between RANK and DENSE_RANK

RANK skips numbers on ties, DENSE_RANK does not.

When to use PARTITION BY

When you want calculations within groups.

Are window functions used in interviews

Yes, especially for ranking and analytics problems.

Internal Learning Links

  • Learn Aggregations
  • Learn GROUP BY
  • Learn Subqueries

Practice CTA

Practice window function problems on Schoolabe to master advanced SQL queries.

Window Functions Missions

Solve exercises in sequence to unlock the next mission.

1

Row number by salary

Assign row number to employees based on salary (highest first).

Solve Mission
2

Rank by salary

Rank employees by salary using RANK().

Locked
3

Dense rank by salary

Rank employees without gaps using DENSE_RANK().

Locked
4

Rank within department

Rank employees within each department.

Locked
5

Running total

Calculate running total of salaries.

Locked
6

Average salary window

Show average salary for all employees using window function.

Locked
7

Previous salary

Show previous salary using LAG().

Locked
8

Next salary

Show next salary using LEAD().

Locked
9

Top 2 per department

Find top 2 employees in each department.

Locked
10

Top per department

Find employee with highest salary in each department.

Locked