Window frames let you control which rows are included when calculating values over a group of rows. This helps you get running totals, moving averages, and other calculations that depend on nearby rows.
Window frame (ROWS BETWEEN, RANGE BETWEEN) in PostgreSQL
window_function() OVER (PARTITION BY column ORDER BY column ROWS BETWEEN frame_start AND frame_end) window_function() OVER (PARTITION BY column ORDER BY column RANGE BETWEEN frame_start AND frame_end)
ROWS BETWEEN counts rows exactly before or after the current row.
RANGE BETWEEN considers values in the ORDER BY column and includes rows with values in a range around the current row's value.
SUM(sales) OVER (ORDER BY date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
AVG(score) OVER (PARTITION BY student_id ORDER BY test_date RANGE BETWEEN INTERVAL '7 days' PRECEDING AND CURRENT ROW)
COUNT(*) OVER (ORDER BY salary RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
This example creates a sales table and inserts 5 days of sales. It then calculates two running sums:
- running_sum_rows: sums the current and two previous rows by date.
- running_sum_range: sums amounts for sales within 2 days before the current sale date.
CREATE TABLE sales_data ( id SERIAL PRIMARY KEY, sale_date DATE, amount INT ); INSERT INTO sales_data (sale_date, amount) VALUES ('2024-01-01', 100), ('2024-01-02', 200), ('2024-01-03', 300), ('2024-01-04', 400), ('2024-01-05', 500); SELECT sale_date, amount, SUM(amount) OVER (ORDER BY sale_date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS running_sum_rows, SUM(amount) OVER (ORDER BY sale_date RANGE BETWEEN INTERVAL '2 days' PRECEDING AND CURRENT ROW) AS running_sum_range FROM sales_data ORDER BY sale_date;
Use ROWS when you want to count a fixed number of rows before or after the current row.
Use RANGE when you want to include rows based on the value range in the ORDER BY column, which can include multiple rows with the same value.
Be careful: RANGE requires the ORDER BY column to be numeric, date, or interval type for meaningful ranges.
Window frames control which rows are included in window function calculations.
ROWS BETWEEN counts rows by position relative to the current row.
RANGE BETWEEN counts rows by value range in the ORDER BY column.
Practice
ROWS BETWEEN clause specify in a window function?Solution
Step 1: Understand window frame types
Window frames can be defined by position (ROWS) or by value range (RANGE).Step 2: Identify what ROWS BETWEEN does
ROWS BETWEEN counts rows relative to the current row's position, not by values.Final Answer:
It defines the frame by counting rows relative to the current row position. -> Option AQuick Check:
ROWS BETWEEN = position-based frame [OK]
- Confusing ROWS with RANGE
- Thinking ROWS filters rows
- Assuming ROWS sorts data
RANGE BETWEEN from 5 preceding to current row?Solution
Step 1: Recall RANGE BETWEEN syntax
The syntax is: RANGE BETWEEN <start> AND <end>, where start must be before end logically.Step 2: Check order of frame boundaries
5 PRECEDING comes before CURRENT ROW, so 'RANGE BETWEEN 5 PRECEDING AND CURRENT ROW' is correct.Final Answer:
RANGE BETWEEN 5 PRECEDING AND CURRENT ROW -> Option AQuick Check:
Correct RANGE syntax = RANGE BETWEEN 5 PRECEDING AND CURRENT ROW [OK]
- Reversing frame boundaries order
- Using ROWS syntax with RANGE
- Omitting BETWEEN keyword
sales(date, amount) ordered by date, what will the following query return?SELECT date, amount, SUM(amount) OVER (ORDER BY date RANGE BETWEEN INTERVAL '1 day' PRECEDING AND CURRENT ROW) AS running_sum FROM sales;
Solution
Step 1: Understand RANGE BETWEEN with INTERVAL
RANGE BETWEEN INTERVAL '1 day' PRECEDING AND CURRENT ROW includes rows with date values within 1 day before current row's date.Step 2: Interpret the SUM window function
SUM(amount) will add amounts for rows dated from 1 day before up to current date.Final Answer:
Sum of amounts for the current date and the previous day -> Option CQuick Check:
RANGE with INTERVAL sums by date range [OK]
- Confusing RANGE with ROWS counting
- Assuming ROWS behavior for RANGE
- Ignoring INTERVAL meaning
SUM(amount) OVER (ORDER BY date ROWS BETWEEN CURRENT ROW AND 3 PRECEDING)
Solution
Step 1: Check frame boundary order
Frame boundaries must go from earlier to later, so '3 PRECEDING' should be before 'CURRENT ROW'.Step 2: Validate syntax correctness
Using ROWS with ORDER BY date and SUM is valid, so no other syntax errors.Final Answer:
The frame boundaries are reversed; '3 PRECEDING' should come before 'CURRENT ROW'. -> Option DQuick Check:
Frame boundaries order matters [OK]
- Reversing frame boundaries
- Thinking ROWS can't use ORDER BY
- Misunderstanding window function usage
sales table has multiple sales per day. Which window frame clause correctly computes this using RANGE?Solution
Step 1: Understand the requirement
We want a 7-day moving average including current day, so the frame covers 6 days before plus current day (total 7 days).Step 2: Choose correct frame type
Since multiple sales per day exist, using RANGE with INTERVAL ensures all rows within date range are included.Step 3: Select correct interval
INTERVAL '6 days' PRECEDING to CURRENT ROW covers 7 days total (6 before + current).Final Answer:
RANGE BETWEEN INTERVAL '6 days' PRECEDING AND CURRENT ROW -> Option BQuick Check:
Use RANGE with 6 days preceding for 7-day window [OK]
- Using ROWS which counts rows, not days
- Choosing wrong interval length
- Confusing 6 days preceding with 7 days
Ready to prove it?
Pass the test and this pattern moves unknown
