Can you explain the concept of window functions and give examples of their analytical uses, such as ranking employees by salary or comparing current vs. previous rows?

Last updated 13 July 2026

Window functions perform calculations across a related set of rows while keeping each row intact, which makes them ideal for analytics like ranking, running totals, and row-to-row comparisons that plain aggregates cannot express.

  • Ranking functions: ROW_NUMBER() (unique sequence), RANK() (gaps on ties), DENSE_RANK() (no gaps): e.g. rank employees by salary within a department.

  • Offset/navigation functions: LAG() and LEAD() access previous/next rows: e.g. compare a month's revenue to the prior month.

  • Aggregate windows: SUM(), AVG() over a frame for running totals and moving averages.

  • Distribution functions: NTILE(), PERCENT_RANK(), CUME_DIST() for buckets and percentiles.

[lang:sql] SELECT name, salary, RANK() OVER (ORDER BY salary DESC) AS salary_rank, salary - LAG(salary) OVER (ORDER BY salary DESC) AS diff_to_prev FROM emp;

  • https://www.devart.com/blog/sql-interview-questions-and-answers.html
  • https://interviewkickstart.com/blogs/interview-questions/sql-interview-questions
  • https://www.simplilearn.com/top-sql-interview-questions-and-answers-article
  • https://intellipaat.com/blog/interview-question/sql-interview-questions/
  • https://www.naukri.com/code360/library/sql-interview-questions
  • https://www.weekday.works/post/advanced-sql-interview-questions
  • https://interviewkickstart.com/blogs/interview-questions/sql-interview-questions-for-experienced-professionals
  • https://medium.com/@anjalimishraa17/top-50-sql-database-interview-questions-the-complete-deep-dive-c5a6c9c1d728
  • https://www.tryexponent.com/blog/data-engineering-interview
  • https://www.interviewquery.com/interview-guides/lime-data-scientist
All Advanced SQL interview questions