What does the ROWS BETWEEN clause specify in a window function?
It defines the frame of rows relative to the current row by counting physical rows before and after it.
Click to reveal answer
intermediate
How does RANGE BETWEEN differ from ROWS BETWEEN in window frames?
RANGE BETWEEN defines the frame based on logical values of the ORDER BY column, including all rows with the same value, while ROWS BETWEEN counts physical rows.
Click to reveal answer
beginner
What is the default window frame if none is specified?
The default is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, meaning from the first row with the same ORDER BY value up to the current row.
Click to reveal answer
beginner
Explain the effect of ROWS BETWEEN 2 PRECEDING AND CURRENT ROW in a window function.
It includes the current row and the two rows before it in the frame for calculation.
Click to reveal answer
intermediate
Why might RANGE BETWEEN cause unexpected results with non-unique ORDER BY values?
Because it includes all rows with the same ORDER BY value, the frame can be larger than expected, affecting aggregates.
Click to reveal answer
What does ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW mean?
AFrom two rows before to two rows after the current row
BFrom the current row to the last row in the partition
COnly the current row
DFrom the first row in the partition to the current row
✗ Incorrect
This frame includes all rows from the start of the partition up to the current row.
Which window frame clause counts physical rows relative to the current row?
ARANGE BETWEEN
BROWS BETWEEN
CGROUPS BETWEEN
DPARTITION BY
✗ Incorrect
ROWS BETWEEN counts physical rows before and after the current row.
If ORDER BY column has duplicates, which frame clause includes all rows with the same value?
ARANGE BETWEEN
BPARTITION BY
CROWS BETWEEN
DORDER BY
✗ Incorrect
RANGE BETWEEN includes all rows with the same ORDER BY value in the frame.
What is the default frame when using window functions with ORDER BY but no frame clause?
ARANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
BNo frame is applied
CROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
DROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
✗ Incorrect
The default frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.
Which clause would you use to include exactly 3 rows before and the current row in a window frame?
ARANGE BETWEEN 3 PRECEDING AND CURRENT ROW
BROWS BETWEEN CURRENT ROW AND 3 FOLLOWING
CROWS BETWEEN 3 PRECEDING AND CURRENT ROW
DRANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
✗ Incorrect
ROWS BETWEEN 3 PRECEDING AND CURRENT ROW includes the current row and 3 rows before it.
Describe the difference between ROWS BETWEEN and RANGE BETWEEN in window frames.
Think about counting rows versus counting values.
You got /4 concepts.
Explain why the default window frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW and what it means for aggregate calculations.
Consider how cumulative sums or averages work.
You got /4 concepts.
Practice
(1/5)
1. What does the ROWS BETWEEN clause specify in a window function?
easy
A. It defines the frame by counting rows relative to the current row position.
B. It defines the frame by counting rows based on the value range in the ORDER BY column.
C. It filters rows before applying the window function.
D. It sorts the rows before applying the 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 A
Quick Check:
ROWS BETWEEN = position-based frame [OK]
Hint: ROWS counts rows by position, RANGE counts by value range [OK]
Common Mistakes:
Confusing ROWS with RANGE
Thinking ROWS filters rows
Assuming ROWS sorts data
2. Which of the following is the correct syntax to specify a window frame using RANGE BETWEEN from 5 preceding to current row?
easy
A. RANGE BETWEEN 5 PRECEDING AND CURRENT ROW
B. RANGE BETWEEN CURRENT ROW AND 5 PRECEDING
C. ROWS BETWEEN 5 PRECEDING AND CURRENT ROW
D. ROWS BETWEEN CURRENT ROW AND 5 PRECEDING
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 A
Quick Check:
Correct RANGE syntax = RANGE BETWEEN 5 PRECEDING AND CURRENT ROW [OK]
Hint: Frame boundaries go from earlier to later, e.g., 5 PRECEDING to CURRENT ROW [OK]
Common Mistakes:
Reversing frame boundaries order
Using ROWS syntax with RANGE
Omitting BETWEEN keyword
3. Given the table 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;
medium
A. Sum of amounts for the current row and 1 row before
B. Sum of all amounts up to the current row
C. Sum of amounts for the current date and the previous day
D. Sum of amounts for the current date only
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 C
Quick Check:
RANGE with INTERVAL sums by date range [OK]
Hint: RANGE with INTERVAL sums values in date/time range [OK]
Common Mistakes:
Confusing RANGE with ROWS counting
Assuming ROWS behavior for RANGE
Ignoring INTERVAL meaning
4. Identify the error in this window frame clause:
SUM(amount) OVER (ORDER BY date ROWS BETWEEN CURRENT ROW AND 3 PRECEDING)
medium
A. The clause is syntactically correct.
B. ROWS cannot be used with ORDER BY date.
C. SUM cannot be used as a window function.
D. The frame boundaries are reversed; '3 PRECEDING' should come before 'CURRENT ROW'.
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 D
Quick Check:
Frame boundaries order matters [OK]
Hint: Frame start must be before frame end [OK]
Common Mistakes:
Reversing frame boundaries
Thinking ROWS can't use ORDER BY
Misunderstanding window function usage
5. You want to calculate a moving average of sales amounts over the last 7 days including the current day, but your sales table has multiple sales per day. Which window frame clause correctly computes this using RANGE?
hard
A. RANGE BETWEEN INTERVAL '7 days' PRECEDING AND CURRENT ROW
B. RANGE BETWEEN INTERVAL '6 days' PRECEDING AND CURRENT ROW
C. ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
D. ROWS BETWEEN 7 PRECEDING AND CURRENT ROW
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 B
Quick Check:
Use RANGE with 6 days preceding for 7-day window [OK]
Hint: RANGE with INTERVAL covers value ranges, perfect for dates [OK]
Common Mistakes:
Using ROWS which counts rows, not days
Choosing wrong interval length
Confusing 6 days preceding with 7 days
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.