Explain SQL window functions with examples: ranking employees by salary and comparing current and previous rows

Quick Overview

Asks what SQL window functions are and how analysts use them, such as ranking employees by salary within a department or comparing each row with the previous one. It tests the OVER clause, how ranking functions treat ties, LAG and running totals, and how to filter on a window result.

Explain SQL window functions with examples: ranking employees by salary and comparing current and previous rows

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

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? ```hint Start from GROUP BY Compare what `GROUP BY` does to the number of rows with what you need when every employee should keep a row and also get a rank. Then think about which parts of the `OVER` clause decide which rows each calculation can see. ``` ### What a Strong Answer Covers - How a window function differs from a `GROUP BY` aggregate in what happens to the rows - The parts of the `OVER` clause: partitioning, ordering and the frame - The ranking functions and how each one treats ties - Offset functions for comparing a row with earlier or later rows, and aggregate windows for running totals or moving averages - Where window functions run in query evaluation, and what that means for filtering on them ### Follow-up Questions - How would you return the top 3 earners in each department, and how do ties change which ranking function you use? - What is the default frame when a window has `ORDER BY`, and how does it surprise people with `LAST_VALUE` or with running totals over tied values? - Why can't a window function appear in `WHERE`, and how do you filter on one? - How would you compute a 7-day moving average when some days have no rows?

Overview: Asks what SQL window functions are and how analysts use them, such as ranking employees by salary within a department or comparing each row with the previous one. It tests the OVER clause, how ranking functions treat ties, LAG and running totals, and how to filter on a window result.

General logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

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?

What a Strong Answer Covers Guidance

  • How a window function differs from a GROUP BY aggregate in what happens to the rows
  • The parts of the OVER clause: partitioning, ordering and the frame
  • The ranking functions and how each one treats ties
  • Offset functions for comparing a row with earlier or later rows, and aggregate windows for running totals or moving averages
  • Where window functions run in query evaluation, and what that means for filtering on them

Follow-up Questions Guidance

  • How would you return the top 3 earners in each department, and how do ties change which ranking function you use?
  • What is the default frame when a window has ORDER BY , and how does it surprise people with LAST_VALUE or with running totals over tied values?
  • Why can't a window function appear in WHERE , and how do you filter on one?
  • How would you compute a 7-day moving average when some days have no rows?