What does the ROWS BETWEEN clause specify in a window function?
It defines the subset of rows (the frame) around the current row to include when calculating the window function.
Click to reveal answer
intermediate
Explain the difference between ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW and ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING.
UNBOUNDED PRECEDING AND CURRENT ROW includes all rows from the start of the partition up to the current row. 1 PRECEDING AND 1 FOLLOWING includes only the row before, the current row, and the row after.
Click to reveal answer
intermediate
What happens if you omit the ROWS BETWEEN clause in a window function?
The default frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, which may behave differently especially with duplicate values.
Click to reveal answer
beginner
Can ROWS BETWEEN frame include future rows relative to the current row?
Yes, by using FOLLOWING you can include rows after the current row in the frame.
Click to reveal answer
beginner
Write a simple SQL query using ROWS BETWEEN 2 PRECEDING AND CURRENT ROW to calculate a moving sum.
SELECT date, sales, SUM(sales) OVER (ORDER BY date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS moving_sum FROM sales_table;
Click to reveal answer
What does ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW mean?
AAll rows from the start of the partition up to the current row
BOnly the current row
COnly the rows after the current row
DOnly the previous row
✗ Incorrect
This frame includes all rows from the beginning of the partition until the current row.
Which keyword specifies rows after the current row in a window frame?
APRECEDING
BUNBOUNDED
CFOLLOWING
DCURRENT
✗ Incorrect
FOLLOWING means rows after the current row.
If you want to include only the current row and the one before it, which frame would you use?
AROWS BETWEEN CURRENT ROW AND 1 FOLLOWING
BROWS BETWEEN 1 PRECEDING AND CURRENT ROW
CROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
DROWS BETWEEN 2 PRECEDING AND CURRENT ROW
✗ Incorrect
This frame includes the row before and the current row.
What is the default frame if ROWS BETWEEN is not specified?
ARANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
BROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
CROWS BETWEEN CURRENT ROW AND CURRENT ROW
DRANGE BETWEEN CURRENT ROW AND CURRENT ROW
✗ Incorrect
The default frame uses RANGE mode from the start of the partition to the current row.
Which of these is a valid frame clause?
AROWS BETWEEN 3 FOLLOWING AND 1 PRECEDING
BROWS BETWEEN UNBOUNDED FOLLOWING AND CURRENT ROW
CROWS BETWEEN CURRENT ROW AND UNBOUNDED PRECEDING
DROWS BETWEEN 1 PRECEDING AND 2 FOLLOWING
✗ Incorrect
Frames must specify a start before the end; 1 PRECEDING to 2 FOLLOWING is valid.
Describe how the ROWS BETWEEN clause controls which rows are included in a window function calculation.
Think about how you select a group of rows around the current row.
You got /4 concepts.
Explain the difference between ROWS and RANGE in window frame specifications.
Consider how duplicates and ordering affect the frame.
You got /4 concepts.
Practice
(1/5)
1. What does the ROWS BETWEEN clause specify in a window function?
easy
A. The range of rows to include relative to the current row for calculation
B. The order in which rows are sorted before aggregation
C. The columns to group by in the query
D. The filter condition for rows in the table
Solution
Step 1: Understand window function frame
The window frame defines which rows are considered for each calculation in a window function.
Step 2: Role of ROWS BETWEEN
The ROWS BETWEEN clause specifies the exact range of rows relative to the current row to include in the calculation.
Final Answer:
The range of rows to include relative to the current row for calculation -> Option A
Quick Check:
ROWS BETWEEN controls row range = D [OK]
Hint: ROWS BETWEEN defines which rows to include around current row [OK]
Common Mistakes:
Confusing ROWS BETWEEN with ORDER BY
Thinking it filters rows from the table
Mixing it up with GROUP BY
2. Which of the following is the correct syntax to specify a window frame from 2 rows before to current row using ROWS BETWEEN?
easy
A. ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
B. ROWS BETWEEN CURRENT ROW AND 2 PRECEDING
C. ROWS BETWEEN 2 FOLLOWING AND CURRENT ROW
D. ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING
Solution
Step 1: Understand frame boundaries order
The syntax is ROWS BETWEEN X AND Y where X is the start and Y is the end of the frame.
Step 2: Identify correct order for 2 rows before to current
Since 2 rows before is the start and current row is the end, it must be ROWS BETWEEN 2 PRECEDING AND CURRENT ROW.
Final Answer:
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW -> Option A
Quick Check:
Start to end order matters = A [OK]
Hint: Start boundary comes first, then end boundary in ROWS BETWEEN [OK]
Common Mistakes:
Reversing the order of boundaries
Using FOLLOWING instead of PRECEDING incorrectly
Omitting CURRENT ROW keyword
3. Given the table sales(date, amount) ordered by date, what is the output of this query?
SELECT date, amount, SUM(amount) OVER (ORDER BY date ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS running_sum FROM sales;
Hint: Sum includes current and 1 previous row amounts [OK]
Common Mistakes:
Including 2 rows before instead of 1
Ignoring the order of rows
Assuming NULL for first row sum
4. Identify the error in this SQL window function:
SELECT id, value, AVG(value) OVER (ORDER BY id ROWS BETWEEN CURRENT ROW AND 2 PRECEDING) AS avg_val FROM data;
medium
A. ORDER BY cannot be used with ROWS BETWEEN
B. The frame boundaries are reversed; start must be before end
C. Missing PARTITION BY clause
D. AVG cannot be used as a window function
Solution
Step 1: Check frame boundaries order
The frame must start with the earlier row and end with the later row. Here, CURRENT ROW is after 2 PRECEDING, so order is reversed.
Step 2: Identify correct syntax
The correct syntax is ROWS BETWEEN 2 PRECEDING AND CURRENT ROW, not the other way around.
Final Answer:
The frame boundaries are reversed; start must be before end -> Option B
Quick Check:
Frame start must be before end = C [OK]
Hint: Start boundary must be earlier than end boundary [OK]
Common Mistakes:
Swapping start and end boundaries
Assuming PARTITION BY is mandatory
Thinking ORDER BY disallows ROWS BETWEEN
5. You want to calculate a moving average of the last 3 sales amounts including the current row, but your data has gaps in dates. Which window frame specification ensures exactly 3 rows are included regardless of date gaps?
hard
A. RANGE BETWEEN INTERVAL '2' DAY PRECEDING AND CURRENT ROW
B. RANGE BETWEEN 2 PRECEDING AND CURRENT ROW
C. ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING
D. ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
Solution
Step 1: Understand difference between ROWS and RANGE
ROWS counts physical rows, while RANGE counts logical values like dates, which can skip rows if dates have gaps.
Step 2: Choose frame to include 3 rows
Using ROWS BETWEEN 2 PRECEDING AND CURRENT ROW always includes exactly 3 rows regardless of date gaps.
Final Answer:
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW -> Option D
Quick Check:
ROWS counts rows, RANGE counts values = B [OK]
Hint: Use ROWS to count exact rows, not RANGE for gaps [OK]
Common Mistakes:
Using RANGE which depends on value gaps
Confusing FOLLOWING with PRECEDING
Assuming RANGE counts rows
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.