0
0
SQLquery~5 mins

Window frame specification (ROWS BETWEEN) in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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.<br>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
Which keyword specifies rows after the current row in a window frame?
APRECEDING
BUNBOUNDED
CFOLLOWING
DCURRENT
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
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
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
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.