Challenge - 5 Problems
Master of Moving Averages
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Calculate 3-day moving average using ROWS frame
Given a sales table with columns
sale_date and amount, what is the output of this query?SELECT sale_date, amount,
AVG(amount) OVER (ORDER BY sale_date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS moving_avg
FROM sales
ORDER BY sale_date;SQL
CREATE TABLE sales (sale_date DATE, amount INT); INSERT INTO sales VALUES ('2024-01-01', 100), ('2024-01-02', 200), ('2024-01-03', 300), ('2024-01-04', 400), ('2024-01-05', 500);
Attempts:
2 left
💡 Hint
Think about how ROWS BETWEEN 2 PRECEDING AND CURRENT ROW includes the current row and two before it.
✗ Incorrect
The window frame includes the current row and the two previous rows, so the average is calculated over up to 3 rows. For the first row, only one row exists, so average is 100. For the second row, average of 100 and 200 is 150, and so on.
❓ query_result
intermediate2:00remaining
Calculate 3-day moving average using RANGE frame
Given the same sales table, what is the output of this query?
SELECT sale_date, amount,
AVG(amount) OVER (ORDER BY sale_date RANGE BETWEEN INTERVAL '2' DAY PRECEDING AND CURRENT ROW) AS moving_avg
FROM sales
ORDER BY sale_date;SQL
CREATE TABLE sales (sale_date DATE, amount INT); INSERT INTO sales VALUES ('2024-01-01', 100), ('2024-01-02', 200), ('2024-01-03', 300), ('2024-01-04', 400), ('2024-01-05', 500);
Attempts:
2 left
💡 Hint
RANGE frame includes rows within 2 days before the current sale_date.
✗ Incorrect
The RANGE frame includes all rows with sale_date within 2 days before and including the current row's date. Since dates are consecutive, this matches the last 3 days for each row, similar to ROWS frame here.
📝 Syntax
advanced1:30remaining
Identify the syntax error in window frame clause
Which option contains a syntax error in the window frame clause of this query?
SELECT sale_date, amount,
AVG(amount) OVER (ORDER BY sale_date ROWS BETWEEN CURRENT ROW AND 2 PRECEDING) AS moving_avg
FROM sales;Attempts:
2 left
💡 Hint
The frame must start with a lower boundary and end with a higher boundary.
✗ Incorrect
The syntax requires the frame to be defined from an earlier row to a later row. 'CURRENT ROW AND 2 PRECEDING' reverses the order and is invalid.
❓ optimization
advanced2:00remaining
Optimize moving average calculation for large dataset
You have a large sales table with millions of rows. Which approach is most efficient to calculate a 7-day moving average of
amount ordered by sale_date?Attempts:
2 left
💡 Hint
Consider how the data is distributed and the type of frame that matches date intervals.
✗ Incorrect
RANGE frame with INTERVAL '6' DAY PRECEDING matches the date range exactly and can use indexes efficiently. ROWS frame counts rows, which may not correspond to days if dates are missing. Correlated subqueries and self joins are slower for large data.
🧠 Conceptual
expert2:30remaining
Understanding difference between ROWS and RANGE frames
Which statement correctly explains the difference between ROWS and RANGE window frames in moving average calculations?
Attempts:
2 left
💡 Hint
Think about how each frame defines the window based on position vs value.
✗ Incorrect
ROWS frame defines the window by counting a fixed number of rows before and after the current row. RANGE frame defines the window by including all rows with ORDER BY values within a specified range relative to the current row's value.