SQL15 min read

SQL Window Functions for Data Professionals

Learn SQL Window Functions with beginner-friendly explanations, examples, interview questions, ranking functions, LEAD, LAG, running totals, and real-world business use cases.

2026-05-15

Part of Series

SQL Fundamentals

Progress

1/1

Current Article

SQL Window Functions for Data Professionals

Part 1

Complete Guide to SQL Window Functions for Data Professionals

If you are learning SQL for Data Engineering, Data Analysis, Data Science, or BI roles, then Window Functions are one of the most important concepts you must master.

Most beginners learn GROUP BY first. But when they start solving real-world business problems, they quickly realize:

  • I want totals without losing row-level data
  • I want to rank employees
  • I want previous row values
  • I want running totals
  • I want top products in each category

That is exactly where Window Functions become powerful.

In this blog, we will learn Window Functions from scratch with:

  • Beginner-friendly explanations
  • Real examples
  • Step-by-step breakdowns
  • Business use cases
  • Interview-level understanding

By the end of this blog, you will be able to confidently use Window Functions in real projects and interviews.


What Are Window Functions?

A Window Function performs calculations across a set of rows related to the current row without collapsing the rows.

This is the biggest difference between GROUP BY and Window Functions.

GROUP BYWindow Function
Reduces rowsKeeps original rows
Gives one output per groupGives output for every row
Used for aggregationUsed for analytics

Understanding with a Simple Example

Imagine we have employee salary data.

emp_idnamedepartmentsalary
1RahulIT50000
2PriyaIT70000
3AmitHR40000
4SnehaHR45000

Problem Statement

We want:

  • Employee details
  • Along with average salary of their department

If we use GROUP BY, we lose employee-level rows.

Using GROUP BY

SELECT department, AVG(salary) AS avg_salary FROM employees GROUP BY department;

Output

departmentavg_salary
IT60000
HR42500

But where did employee rows go?

This is where Window Functions help.


First Window Function Example

SELECT emp_id, name, department, salary, AVG(salary) OVER( PARTITION BY department ) AS dept_avg_salary FROM employees;

Output

emp_idnamedepartmentsalarydept_avg_salary
1RahulIT5000060000
2PriyaIT7000060000
3AmitHR4000042500
4SnehaHR4500042500

Now we still have:

  • Original rows
  • Additional analytics

That is the beauty of Window Functions.


Structure of a Window Function

General syntax:

FUNCTION_NAME(column) OVER( PARTITION BY column ORDER BY column )

Let us understand each part.


OVER() Clause

OVER() tells SQL:

Apply this function as a Window Function.

Without OVER(), it behaves like a normal aggregate function.

Example

AVG(salary)

Normal aggregate.

AVG(salary) OVER()

Window Function.


PARTITION BY

PARTITION BY divides data into groups.

Think of it like:

GROUP BY without collapsing rows.

Example

AVG(salary) OVER( PARTITION BY department )

This means:

  • Create separate windows for each department
  • Calculate average inside each department

ORDER BY in Window Functions

Inside Window Functions, ORDER BY controls row sequence.

Example

SUM(salary) OVER( ORDER BY emp_id )

This creates a running total.


Running Total Example

Query

SELECT emp_id, name, salary, SUM(salary) OVER( ORDER BY emp_id ) AS running_salary FROM employees;

Output

emp_idsalaryrunning_salary
15000050000
270000120000
340000160000
445000205000

Real Business Use Cases

Window Functions are heavily used in:

  • Sales analytics
  • Financial reporting
  • Customer behavior analysis
  • Data warehousing
  • KPI dashboards
  • Time-series analysis
  • Ranking systems

Most Important Window Functions

Every data professional should know these:

  1. ROW_NUMBER()
  2. RANK()
  3. DENSE_RANK()
  4. LEAD()
  5. LAG()
  6. SUM()
  7. AVG()
  8. FIRST_VALUE()
  9. LAST_VALUE()

ROW_NUMBER()

Assigns unique row numbers.

Example

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

Output

namedepartmentsalaryrow_num
PriyaIT700001
RahulIT500002
SnehaHR450001
AmitHR400002

Use Cases

  • Top N records
  • Latest records
  • Duplicate removal

Finding Highest Salary Employee in Each Department

WITH ranked_data AS ( SELECT *, ROW_NUMBER() OVER( PARTITION BY department ORDER BY salary DESC ) AS rn FROM employees ) SELECT * FROM ranked_data WHERE rn = 1;

This is one of the most common SQL interview questions.


RANK()

Similar to ROW_NUMBER(), but ties get the same rank.

Example Data

namesalary
A100
B100
C90

Using RANK()

namerank
A1
B1
C3

Notice:

  • Rank 2 is skipped

DENSE_RANK()

Similar to RANK(), but without gaps.

namedense_rank
A1
B1
C2

Difference Between ROW_NUMBER, RANK, and DENSE_RANK

FunctionDuplicate Rank AllowedSkips Rank
ROW_NUMBERNoNo
RANKYesYes
DENSE_RANKYesNo

LAG()

Gets the previous row value.

Example

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

Output

emp_idsalaryprevious_salary
150000NULL
27000050000
34000070000
44500040000

Real Use Cases

  • Day-over-day sales comparison
  • Stock price movement
  • Revenue growth analysis
  • Customer purchase tracking

LEAD()

Opposite of LAG().

Gets next row value.

Example

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

Difference Between LAG and LEAD

FunctionRetrieves
LAGPrevious row
LEADNext row

Window Frame Concept

A window frame defines:

Which rows should participate in calculation?

Example

SUM(salary) OVER( ORDER BY emp_id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW )

Meaning:

  • Start from first row
  • Continue till current row

This creates cumulative totals.


Important Window Frame Keywords

KeywordMeaning
UNBOUNDED PRECEDINGStart from first row
CURRENT ROWCurrent row
UNBOUNDED FOLLOWINGTill last row

Common Interview Questions

Difference Between GROUP BY and Window Functions

  • GROUP BY reduces rows
  • Window Functions retain rows

Difference Between RANK and DENSE_RANK

  • RANK() skips numbers after ties
  • DENSE_RANK() does not skip numbers

When Should We Use ROW_NUMBER()

Use when unique numbering is needed.

Examples:

  • Remove duplicates
  • Find latest records

Real-World Data Engineering Use Cases

Window Functions are heavily used in:

  • Incremental data pipelines
  • CDC processing
  • Deduplication logic
  • SCD Type 2 implementation
  • Data quality checks
  • KPI calculations

Example: Removing Duplicate Records

WITH duplicate_check AS ( SELECT *, ROW_NUMBER() OVER( PARTITION BY customer_id ORDER BY updated_date DESC ) AS rn FROM customers ) SELECT * FROM duplicate_check WHERE rn = 1;

This keeps only the latest record per customer.

This pattern is used heavily in real-world Data Engineering projects.


Performance Tips

Window Functions are powerful but expensive on huge datasets.

Best practices:

  • Use proper indexing
  • Partition wisely
  • Avoid unnecessary sorting
  • Process data in stages
  • Use distributed engines carefully

Common Mistakes Beginners Make

Forgetting OVER()

Wrong:

AVG(salary)

Correct:

AVG(salary) OVER()

Confusing GROUP BY with PARTITION BY

Remember:

  • GROUP BY collapses rows
  • PARTITION BY keeps rows

Missing ORDER BY in Ranking Functions

Ranking functions require ordering.


20 Best SQL Window Function Practice Questions

If you can solve these 20 questions confidently, you will be comfortable with most real-world and interview-level Window Function problems:

  1. Find the highest-paid employee in each department.

  2. Find the second-highest salary in each department.

  3. Find the top 3 employees from each department based on salary.

  4. Show each employee with their department average salary.

  5. Find employees earning more than their department average salary.

  6. Find duplicate customer records and keep only the latest record.

  7. Find customers whose city changed over time.

  8. Calculate running total of daily sales.

  9. Calculate running total of sales separately for each region.

  10. Compare today’s sales with previous day’s sales.

  11. Calculate month-over-month revenue growth.

  12. Find the first order of every customer.

  13. Find the latest order of every customer.

  14. Find the gap in days between two consecutive orders of a customer.

  15. Find customers who came back after more than 30 days.

  16. Find top 3 products by sales in each category.

  17. Find each product’s contribution percentage to total sales.

  18. Calculate 3-day moving average of sales.

  19. Find users who logged in on consecutive days.

  20. Find the latest status of every order or ticket.


Quick Revision Cheat Sheet

FunctionPurpose
ROW_NUMBER()Unique numbering
RANK()Ranking with gaps
DENSE_RANK()Ranking without gaps
LAG()Previous row
LEAD()Next row
SUM() OVER()Running total
AVG() OVER()Moving averages

Conclusion

Window Functions are not just an interview topic.

They are one of the most practical and frequently used SQL concepts in real-world data projects.

Mastering them will help you:

  • Write advanced SQL queries
  • Solve business problems efficiently
  • Perform analytics professionally
  • Crack data interviews confidently

If you are serious about becoming a strong data professional, Window Functions are absolutely worth mastering deeply.

Soumya Ranjan Bisoyi

Written By

Soumya Ranjan Bisoyi

Data Engineer • Mentor • Educator

Helping aspiring Data Engineers learn SQL, Spark, Snowflake, Azure, and real-world Data Engineering concepts through practical, beginner-friendly content.