Analytical Window Frames (ROWS BETWEEN, RANGE BETWEEN, LAG, LEAD)
Control frame boundaries and peer access with LAG/LEAD for time-series and running analytics.
In plain words
Choose the independent series. Next, sort by time and a stable tie-breaker. Finally, aggregate or inspect prior/next peer.
Think of it like…
ROWS frames a moving camera by pixel count; RANGE frames it by distance on the road, so more or fewer cars can enter when timestamps tie.
Where you’ve seen it
AVG(value) OVER (ORDER BY event_at ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) is exactly seven records, whereas RANGE BETWEEN INTERVAL '6 hours' PRECEDING is time-based and may include peers.
Words you’ll hear
- ROWS/RANGE
- A frame boundary counted in physical rows after the ORDER BY sort, independent of equal ordering values.
- LAG
- A frame boundary based on ORDER BY value distance that includes or excludes peers according to the boundary.
- LEAD
- Value access to a prior or following row in the same partition without a self-join, with an optional default at the edge.
- Operational boundary
- RANGE has peer-sensitive boundaries for duplicate timestamps, and omitting an explicit frame changes under
ORDER BYto a cumulative default in databases whereRANGE UNBOUNDED PRECEDINGis implied. UseROWS BETWEENfor a count of physical rows,RANGE BETWEEN INTERVALfor time width, order uniquely when prior/next values must be deterministic, and state NULL/default behavior for LAG/LEAD.
Visual diagram
Theory & deep dive
ROWS/RANGE: the working mechanism
A window ORDER BY supplies peer order, while a ROWS or RANGE frame selects exactly which preceding and following rows contribute to the aggregate; LAG/LEAD access another partition row directly.
Choose the independent series. Sort by time and a stable tie-breaker, then select rows by count or ordered value; this exposes the state governed by LEAD.
AVG(value) OVER (ORDER BY event_at ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) is exactly seven records, whereas RANGE BETWEEN INTERVAL '6 hours' PRECEDING is time-based and may include peers.
Analytical Window Frames: failure mode and production response
RANGE has peer-sensitive boundaries for duplicate timestamps, and omitting an explicit frame changes under ORDER BY to a cumulative default in databases where RANGE UNBOUNDED PRECEDING is implied.
Use ROWS BETWEEN for a count of physical rows, RANGE BETWEEN INTERVAL for time width, order uniquely when prior/next values must be deterministic, and state NULL/default behavior for LAG/LEAD. Track 7-row mean as "ROWS BETWEEN 6 PRECEDING AND CURRENT ROW", and make Time range visible to the dataset owner.
ROWS BETWEEN for a count of physical rows, RANGE BETWEEN INTERVAL for time width, order uniquely when prior/next values must be deterministic, and state NULL/default behavior for LAG/LEAD.How it works, step by step
Step 1 of 41. Partition
Choose the independent series. A window ORDER BY supplies peer order, while a ROWS or RANGE frame selects exactly which preceding and following rows contribute to the aggregate; LAG/LEAD access another partition row directly.
- 7-row mean
- `ROWS BETWEEN 6 PRECEDING AND CURRENT ROW`
- LAG
- A frame boundary based on ORDER BY value distance that includes or excludes peers according to the boundary.
When to use it & trade-offs
What it is
A window ORDER BY supplies peer order, while a ROWS or RANGE frame selects exactly which preceding and following rows contribute to the aggregate; LAG/LEAD access another partition row directly. AVG(value) OVER (ORDER BY event_at ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) is exactly seven records, whereas RANGE BETWEEN INTERVAL '6 hours' PRECEDING is time-based and may include peers.
Why it matters: RANGE has peer-sensitive boundaries for duplicate timestamps, and omitting an explicit frame changes under ORDER BY to a cumulative default in databases where RANGE UNBOUNDED PRECEDING is implied. Use ROWS BETWEEN for a count of physical rows, RANGE BETWEEN INTERVAL for time width, order uniquely when prior/next values must be deterministic, and state NULL/default behavior for LAG/LEAD.
When to reach for it
- Control frame boundaries and peer access with LAG/LEAD for time-series and running analytics.
- Choose ROWS/RANGE because it provides this capability: A frame boundary counted in physical rows after the ORDER BY sort, independent of equal ordering values.
- Keep evidence for 7-row mean at "
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW" so the last production stage-Evaluate - aggregate or inspect prior/next peer-remains testable.
When to avoid or be careful
- RANGE has peer-sensitive boundaries for duplicate timestamps, and omitting an explicit frame changes under
ORDER BYto a cumulative default in databases whereRANGE UNBOUNDED PRECEDINGis implied. - Do not conflate LAG with ROWS/RANGE. The second concept contributes different behavior: A frame boundary based on ORDER BY value distance that includes or excludes peers according to the boundary.
- Release only after this guardrail is in place: Use
ROWS BETWEENfor a count of physical rows,RANGE BETWEEN INTERVALfor time width, order uniquely when prior/next values must be deterministic, and state NULL/default behavior for LAG/LEAD.
ROWS/RANGE / LAG / LEAD - roles, limits, and operating cost
Strengths: A frame boundary counted in physical rows after the ORDER BY sort, independent of equal ordering values. A window ORDER BY supplies peer order, while a ROWS or RANGE frame selects exactly which preceding and following rows contribute to the aggregate.
Watch out: RANGE has peer-sensitive boundaries for duplicate timestamps, and omitting an explicit frame changes under ORDER BY to a cumulative default in databases where RANGE UNBOUNDED PRECEDING is implied.
Strengths: A frame boundary based on ORDER BY value distance that includes or excludes peers according to the boundary. In this workflow it supports: Frame - select rows by count or ordered value.
Watch out: The operating cost is real: boundary width can contain zero or many tied events.
Strengths: Value access to a prior or following row in the same partition without a self-join, with an optional default at the edge. Its output can be checked with Frame default.
Watch out: Operating guardrail: Use ROWS BETWEEN for a count of physical rows, RANGE BETWEEN INTERVAL for time width, order uniquely when prior/next values must be deterministic, and state NULL/default behavior for LAG/LEAD.
Interview signals & numbers to know
When this topic comes up
- “Where is the failure boundary for ROWS/RANGE, and how would 7-row mean reveal it?”
- “Why is LAG distinct from this behavior: value access to a prior or following row in the same partition without a self-join, with an optional default at the edge?”
- Production clue: RANGE has peer-sensitive boundaries for duplicate timestamps, and omitting an explicit frame changes under ORDER BY to a cumulative default in databases where RANGE UNBOUNDED PRECEDING is implied.
- Design probe: Use ROWS BETWEEN for a count of physical rows, RANGE BETWEEN INTERVAL for time width, order uniquely when prior/next values must be deterministic, and state NULL/default behavior for LAG/LEAD.
Benchmarks & rules of thumb
- 7-row mean
- `ROWS BETWEEN 6 PRECEDING AND CURRENT ROW`
- Time range
- boundary width can contain zero or many tied events
- Frame default
- must not be assumed across ranking and aggregate functions
Common mistakes & follow-ups
Common interview mistakes
- RANGE has peer-sensitive boundaries for duplicate timestamps, and omitting an explicit frame changes under
ORDER BYto a cumulative default in databases whereRANGE UNBOUNDED PRECEDINGis implied. - Reporting success from a job exit code without checking 7-row mean and Time range.
- Treating this example as complete without replay, rollback, or access boundaries:
AVG(value) OVER (ORDER BY event_at ROWS BETWEEN 6 PRECEDING AND CURRENT ROW)is exactly seven records, whereasRANGE BETWEEN INTERVAL '6 hours' PRECEDINGis time-based and may include peers.
Likely interviewer follow-ups
- How would you prove this contract instead of only checking that the command ran: A frame boundary counted in physical rows after the ORDER BY sort, independent of equal ordering values.
- What does the recovery plan do when this occurs? RANGE has peer-sensitive boundaries for duplicate timestamps, and omitting an explicit frame changes under
ORDER BYto a cumulative default in databases whereRANGE UNBOUNDED PRECEDINGis implied. - When is ROWS/RANGE insufficient for Time range, and what does adding LAG cost in 7-row mean?
ROWS BETWEEN for a count of physical rows, RANGE BETWEEN INTERVAL for time width, order uniquely when prior/next values must be deterministic, and state NULL/default behavior for LAG/LEAD.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Analytical Window Frames (ROWS BETWEEN, RANGE BETWEEN, LAG, LEAD) with data engineer interview Q&A and hands-on practice.