0
0
SQLquery~20 mins

Window frame specification (ROWS BETWEEN) in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Window Frame Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of ROWS BETWEEN UNBOUNDED PRECEDING and CURRENT ROW
Given the table sales with columns id and amount, what is the output of the following query?
SELECT id, amount, SUM(amount) OVER (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total FROM sales ORDER BY id;
SQL
CREATE TABLE sales (id INT, amount INT);
INSERT INTO sales VALUES (1, 100), (2, 200), (3, 300);
A[{"id":1,"amount":100,"running_total":300},{"id":2,"amount":200,"running_total":500},{"id":3,"amount":300,"running_total":600}]
B[{"id":1,"amount":100,"running_total":600},{"id":2,"amount":200,"running_total":600},{"id":3,"amount":300,"running_total":600}]
C[{"id":1,"amount":100,"running_total":100},{"id":2,"amount":200,"running_total":300},{"id":3,"amount":300,"running_total":600}]
D[{"id":1,"amount":100,"running_total":100},{"id":2,"amount":200,"running_total":200},{"id":3,"amount":300,"running_total":300}]
Attempts:
2 left
💡 Hint
Think about how the window frame includes all rows from the start up to the current row.
query_result
intermediate
2:00remaining
Effect of ROWS BETWEEN 1 PRECEDING AND CURRENT ROW
What is the output of this query on the sales table?
SELECT id, amount, AVG(amount) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS avg_last_two FROM sales ORDER BY id;
SQL
CREATE TABLE sales (id INT, amount INT);
INSERT INTO sales VALUES (1, 100), (2, 200), (3, 300);
A[{"id":1,"amount":100,"avg_last_two":200.0},{"id":2,"amount":200,"avg_last_two":250.0},{"id":3,"amount":300,"avg_last_two":300.0}]
B[{"id":1,"amount":100,"avg_last_two":100.0},{"id":2,"amount":200,"avg_last_two":150.0},{"id":3,"amount":300,"avg_last_two":250.0}]
C[{"id":1,"amount":100,"avg_last_two":100.0},{"id":2,"amount":200,"avg_last_two":200.0},{"id":3,"amount":300,"avg_last_two":300.0}]
D[{"id":1,"amount":100,"avg_last_two":150.0},{"id":2,"amount":200,"avg_last_two":250.0},{"id":3,"amount":300,"avg_last_two":350.0}]
Attempts:
2 left
💡 Hint
The frame includes the current row and one row before it, except for the first row which has only itself.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in window frame clause
Which option contains a syntax error in the window frame specification?
SELECT id, amount, SUM(amount) OVER (ORDER BY id ROWS BETWEEN CURRENT ROW AND 1 PRECEDING) FROM sales;
AROWS BETWEEN CURRENT ROW AND 1 PRECEDING
BROWS BETWEEN 1 PRECEDING AND CURRENT ROW
CROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
DROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
Attempts:
2 left
💡 Hint
The frame must specify a start point that comes before the end point.
optimization
advanced
2:00remaining
Optimizing window frame for cumulative sum
Which window frame clause is the most efficient for calculating a cumulative sum ordered by id in a large table?
SELECT id, amount, SUM(amount) OVER (ORDER BY id ROWS BETWEEN ? AND CURRENT ROW) FROM sales;
AUNBOUNDED PRECEDING
B1 PRECEDING
CUNBOUNDED FOLLOWING
DCURRENT ROW
Attempts:
2 left
💡 Hint
Cumulative sum means adding all rows from the start up to the current row.
🧠 Conceptual
expert
3:00remaining
Understanding frame exclusion with ROWS BETWEEN
Consider the query:
SELECT id, amount, SUM(amount) OVER (ORDER BY id ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING) AS sum_excluded_current FROM sales ORDER BY id;

What does this window frame exclude from the sum calculation?
AThe current row and the immediately preceding row
BOnly the current row
CThe current row and the two preceding rows
DThe current row and all following rows
Attempts:
2 left
💡 Hint
The frame includes rows from 2 before to 1 before the current row, excluding the current row itself.