SQL window frame clause: ROWS vs RANGE, when to use each, and the duplicate-peers trap with RANGE

Quick Overview

Asks what the frame clause of a SQL window function controls and how ROWS and RANGE frames choose rows. Tests running totals over tied ORDER BY values, the default frame, date-based rolling windows, and keeping window results deterministic.

SQL window frame clause: ROWS vs RANGE, when to use each, and the duplicate-peers trap with RANGE

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

What is the frame clause in a window function? Explain the difference between `ROWS` and `RANGE` frames, when to use each, and the duplicate-peers trap with `RANGE`. ```hint The frame you did not write Work out which frame the database uses when `OVER (...)` has an `ORDER BY` but no frame clause at all. ``` ```hint Compute one by hand Take a running total over orders where two rows share the same order date, and fill in the result for each row under `ROWS` and under `RANGE`. ``` ### What a Strong Answer Covers - What the frame clause is and how it relates to `PARTITION BY` and `ORDER BY` - How `ROWS` and `RANGE` decide which rows fall in the frame, including how tied rows are treated - A fitting use case for each mode, including value offsets over dates - The default frame and how it produces the duplicate-peers result in a running total - Whether the result is deterministic when the `ORDER BY` key has ties ### Follow-up Questions - What does `LAST_VALUE(x) OVER (ORDER BY d)` return, and why does it surprise people? - You need a 7-day rolling sum of revenue, but some days have no rows at all. How do `ROWS` and `RANGE` frames behave, and how would you fix the `ROWS` version? - How does the `GROUPS` frame mode differ from `ROWS` and `RANGE`? - Which window functions ignore the frame clause entirely?

Overview: Asks what the frame clause of a SQL window function controls and how ROWS and RANGE frames choose rows. Tests running totals over tied ORDER BY values, the default frame, date-based rolling windows, and keeping window results deterministic.

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

What is the frame clause in a window function? Explain the difference between ROWS and RANGE frames, when to use each, and the duplicate-peers trap with RANGE.

What a Strong Answer Covers Guidance

  • What the frame clause is and how it relates to PARTITION BY and ORDER BY
  • How ROWS and RANGE decide which rows fall in the frame, including how tied rows are treated
  • A fitting use case for each mode, including value offsets over dates
  • The default frame and how it produces the duplicate-peers result in a running total
  • Whether the result is deterministic when the ORDER BY key has ties

Follow-up Questions Guidance

  • What does LAST_VALUE(x) OVER (ORDER BY d) return, and why does it surprise people?
  • You need a 7-day rolling sum of revenue, but some days have no rows at all. How do ROWS and RANGE frames behave, and how would you fix the ROWS version?
  • How does the GROUPS frame mode differ from ROWS and RANGE ?
  • Which window functions ignore the frame clause entirely?