Learn SQL
-- windows.window_frames

Window Frames: ROWS / RANGE + NTILE in SQL

Moving averages, quartiles, and the LAST_VALUE trap

--concept

Frames bound participating peers: ROWS counts physical rows (precise, stepwise); RANGE groups ORDER BY peers (ties move together); GROUPS groups peer sets. Default RANGE-to-current-peer surprises with duplicate keys β€” explicit ROWS is predictable. FIRST_VALUE/LAST_VALUE/NTH_VALUE pick positions; bare LAST_VALUE returns the current row (frame ends there) unless given UNBOUNDED FOLLOWING. NTILE(n) buckets into n near-equal groups.

--how it works
1

ROWS is physical

Counts rows β€” precise, stepwise. The predictable choice.

2

RANGE groups peers

Ties on ORDER BY move together β€” totals jump on duplicate keys.

3

LAST_VALUE trap

Default frame ends at current row β€” bare LAST_VALUE returns current row, not partition end. Needs UNBOUNDED FOLLOWING.

4

NTILE buckets

Splits ordered rows into n near-equal groups; remainders to early buckets. Empty OVER () is whole-result window.

--visualization
namehalf
Cynthia1
Alice1
Farid1
Elena2
Bob2
David2
Stage 1 / 1
Six rows β†’ 3+3 halves. Remainders go to early buckets.
--example
query.sql
1SELECT name, NTILE(2) OVER (ORDER BY salary DESC) AS half FROM employees ORDER BY salary DESC;
Result
namehalf
Cynthia1
Alice1
Farid1
Elena2
Bob2
David2

Six rows split into two near-equal halves: top three earners in bucket 1, bottom three in bucket 2. Same OVER shape with ROWS BETWEEN builds moving averages; with LAST_VALUE it must carry the full-partition frame.

--variants

3-row moving average

Explicit frame slides predictably.

query.sql
1SELECT name, AVG(salary) OVER (ORDER BY id ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS moving_avg FROM employees ORDER BY id;
Result
namemoving_avg
Alice85000
Bob68500
Cynthia76000
David63333.33
Elena65333.33
Farid59333.33
1Frame holds at most current + two predecessors.
2At each row newcomer enters, departed leaves.
3Early big salaries slide out β€” Farid dips to 59333.

Halves + last-in-dept

NTILE buckets; LAST_VALUE needs full frame.

query.sql
1SELECT name, NTILE(2) OVER (ORDER BY salary DESC) AS half, LAST_VALUE(salary) OVER (PARTITION BY department ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS last_in_dept FROM employees ORDER BY salary DESC LIMIT 2;
Result
namehalflast_in_dept
Cynthia173000
Alice173000
1NTILE(2) splits six into 3+3 halves.
2LAST_VALUE with full frame reaches partition end (Farid 73k).
3Bare LAST_VALUE would have returned the current row instead.
--key facts
ROWSphysical, stepwise β€” predictable
RANGEpeer-values move together β€” jumps on dupes
LAST_VALUE trapneeds explicit full frame
--use cases
Cumulative revenue, 7-day moving averages, burn-downs
Quartile banding, sessionization with LAG + cumulative flags
--under the hood
Wide frames cost memory; narrow streaming frames stay cheap. Duplicate ORDER keys batch peers under RANGE β€” use ROWS for stepwise.
PERCENT_RANK/CUME_DIST ride the same ordering with relative positioning.

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.