What is a window frame and why does it matter?

Quick Overview

Asks what a window frame is in a SQL window function and why the same aggregate can return a running total, a moving average or a partition total. Tests ROWS versus RANGE frames, the default frame once ORDER BY is added, and how tied rows are handled.

What is a window frame and why does it matter?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

What is a window frame and why does it matter? The question is about SQL window functions such as `SUM(amount) OVER (PARTITION BY user_id ORDER BY order_date)`. ```hint Compare two queries Work out what `SUM(amount) OVER (PARTITION BY user_id)` returns for each row, then add `ORDER BY order_date` inside the `OVER` and ask which rows are being added now. ``` ```hint Duplicate sort keys Check what a running total does for two rows that share the same `order_date`. ``` ### What a Strong Answer Covers - A definition of the frame relative to the partition and the current row - How a frame is written: `ROWS` versus `RANGE` and the bound keywords - The default frame with and without `ORDER BY`, including how tied rows are treated - Which window functions read the frame and which ignore it - A concrete case where changing only the frame changes the result ### Follow-up Questions - Why does `LAST_VALUE(x) OVER (ORDER BY t)` often return the current row's own value, and how do you fix it? - How would you compute a 7-day moving average when some days have no rows and others have several? - How do `ROWS`, `RANGE` and `GROUPS` frames differ when the sort key has ties?

Overview: Asks what a window frame is in a SQL window function and why the same aggregate can return a running total, a moving average or a partition total. Tests ROWS versus RANGE frames, the default frame once ORDER BY is added, and how tied rows are handled.

PracHub logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

What is a window frame and why does it matter?

The question is about SQL window functions such as SUM(amount) OVER (PARTITION BY user_id ORDER BY order_date).

What a Strong Answer Covers Guidance

  • A definition of the frame relative to the partition and the current row
  • How a frame is written: ROWS versus RANGE and the bound keywords
  • The default frame with and without ORDER BY , including how tied rows are treated
  • Which window functions read the frame and which ignore it
  • A concrete case where changing only the frame changes the result

Follow-up Questions Guidance

  • Why does LAST_VALUE(x) OVER (ORDER BY t) often return the current row's own value, and how do you fix it?
  • How would you compute a 7-day moving average when some days have no rows and others have several?
  • How do ROWS , RANGE and GROUPS frames differ when the sort key has ties?