Explain the default frame for ordered versus unordered SQL window functions and why the default matters

Quick Overview

Asks what frame a SQL window function uses by default with and without ORDER BY, and why that default changes results. Tests running totals versus group totals, how ROWS and RANGE treat tied rows, the LAST_VALUE trap, and when to write the frame explicitly.

Explain the default frame for ordered versus unordered SQL window functions and why the default matters

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Explain the default frame clause for ordered versus unordered window functions, and why it is important to be aware of the default behavior. ```hint Add one clause Compare `SUM(x) OVER (PARTITION BY dept)` with the same expression after adding `ORDER BY day` inside the `OVER`, and ask which rows each row can see in each case. ``` ```hint Look at ties Ask what happens when two rows in a partition share the same `ORDER BY` value, and whether `ROWS` and `RANGE` frames treat them the same way. ``` ### What a Strong Answer Covers - The default frame when the window has no `ORDER BY` - The default frame once `ORDER BY` is added, stated precisely, including that it is a `RANGE` frame - How `RANGE` treats tied rows compared with `ROWS`, and what `ROWS` needs to give a deterministic result - Which functions the default affects (aggregates, `FIRST_VALUE`, `LAST_VALUE`) and which ignore frames entirely - The bugs the defaults cause in practice, and the habit of writing the frame explicitly ### Follow-up Questions - Why does `LAST_VALUE(price) OVER (PARTITION BY ticker ORDER BY ts)` not return the partition's last price, and how do you fix it? - How would you compute a 7-day moving average when some days have no rows, and which frame type would you use? - Does a frame clause change the output of `ROW_NUMBER` or `LAG`? - Two rows tie on the `ORDER BY` column and the frame uses `ROWS`. Is the result deterministic?

Overview: Asks what frame a SQL window function uses by default with and without ORDER BY, and why that default changes results. Tests running totals versus group totals, how ROWS and RANGE treat tied rows, the LAST_VALUE trap, and when to write the frame explicitly.

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

Explain the default frame clause for ordered versus unordered window functions, and why it is important to be aware of the default behavior.

What a Strong Answer Covers Guidance

  • The default frame when the window has no ORDER BY
  • The default frame once ORDER BY is added, stated precisely, including that it is a RANGE frame
  • How RANGE treats tied rows compared with ROWS , and what ROWS needs to give a deterministic result
  • Which functions the default affects (aggregates, FIRST_VALUE , LAST_VALUE ) and which ignore frames entirely
  • The bugs the defaults cause in practice, and the habit of writing the frame explicitly

Follow-up Questions Guidance

  • Why does LAST_VALUE(price) OVER (PARTITION BY ticker ORDER BY ts) not return the partition's last price, and how do you fix it?
  • How would you compute a 7-day moving average when some days have no rows, and which frame type would you use?
  • Does a frame clause change the output of ROW_NUMBER or LAG ?
  • Two rows tie on the ORDER BY column and the frame uses ROWS . Is the result deterministic?