What if you could get complex running totals and averages instantly without fiddling with formulas every time?
Why Window frame (ROWS BETWEEN, RANGE BETWEEN) in PostgreSQL? - Purpose & Use Cases
Imagine you have a long list of daily sales numbers in a spreadsheet. You want to find the average sales for each day, but not just for that day alone -- you want to include the days before and after it to see trends. Doing this manually means copying and pasting formulas for each row, adjusting ranges every time.
Manually adjusting formulas for each row is slow and easy to mess up. You might select the wrong range or forget to update the formula when new data arrives. This leads to errors and wastes a lot of time, especially with big data.
Window frames like ROWS BETWEEN and RANGE BETWEEN let you tell the database exactly how many rows or what range of values to include around each row automatically. This means you get running totals, moving averages, and other calculations done correctly and instantly for every row without manual effort.
Calculate average sales for each day by manually adjusting formulas for each row in a spreadsheet.
SELECT day, sales, AVG(sales) OVER (ORDER BY day ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING) AS moving_avg FROM sales_table;
This lets you analyze trends and patterns over sliding windows of data easily, unlocking powerful insights from your data without extra manual work.
A store manager wants to see the average sales over the past 3 days and next 3 days for each day to understand sales trends and plan inventory better.
Manual calculations for moving averages are slow and error-prone.
Window frames automate range-based calculations around each row.
This makes trend analysis and running totals easy and reliable.
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
