Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 26/ 160
02. Advanced SQL & Relational Databases

Analytical Window Frames (ROWS BETWEEN, RANGE BETWEEN, LAG, LEAD)

Control frame boundaries and peer access with LAG/LEAD for time-series and running analytics.

AdvancedROWS/RANGE · LAG · LEAD3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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 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.

Visual diagram

Loading interactive 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.

Analytical Window Frames in one concrete design

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.

Key takeaway: A window ORDER BY supplies peer order, while a ROWS or RANGE frame selects exactly which preceding and following rows contribute to the aggregate

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.

Key takeaway: 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.

How it works, step by step

Step 1 of 4
ROWS/RANGE

1. 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.

choose the independent series
Key numbers at this step
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 BY to a cumulative default in databases where RANGE UNBOUNDED PRECEDING is 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 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.

ROWS/RANGE / LAG / LEAD - roles, limits, and operating cost

ROWS/RANGE
Best for: A frame boundary counted in physical rows after the ORDER BY sort, independent of equal ordering values.

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.

LAG
Best for: A frame boundary based on ORDER BY value distance that includes or excludes peers according to the boundary.

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.

LEAD
Best for: Value access to a prior or following row in the same partition without a self-join, with an optional default at the edge.

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 BY to a cumulative default in databases where RANGE UNBOUNDED PRECEDING is 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, whereas RANGE BETWEEN INTERVAL '6 hours' PRECEDING is 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 BY to a cumulative default in databases where RANGE UNBOUNDED PRECEDING is implied.
  • When is ROWS/RANGE insufficient for Time range, and what does adding LAG cost in 7-row mean?
Interview tip: Lead with the data path and failure boundary: 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. Then make the answer operational: 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.

Knowledge check

Loading quiz

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.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
Window Functions (ROW_NUMBER, RANK, DENSE_RANK, NTILE)
Next topic
Aggregations, GROUPING SETS, ROLLUP & CUBE