Questions: LAG, LEAD, and OFFSET: Accessing Rows in Windows

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

You want to compute month-over-month revenue change for each row in a sales table ordered by month. Which approach is correct?

ASELECT month, revenue - LAG(revenue, 1) OVER (ORDER BY month) AS mom_change FROM sales
BSELECT month, revenue - LAG(revenue, 1) FROM sales ORDER BY month
CSELECT s1.month, s1.revenue - s2.revenue AS mom_change FROM sales s1 JOIN sales s2 ON s1.month = s2.month + 1
DSELECT month, revenue - FIRST_VALUE(revenue) OVER (ORDER BY month) AS mom_change FROM sales
Question 2 Multiple Choice

A query uses LAST_VALUE(price) OVER (PARTITION BY product ORDER BY sale_date). What does this return on each row with the default window frame?

AThe price from the very last sale of that product
BThe price from the very first sale of that product
CThe price from the current row itself
DNULL, because LAST_VALUE requires an explicit frame
Question 3 True / False

When a partition boundary is crossed, LAG returns NULL (or the specified default) rather than looking into the previous partition.

TTrue
FFalse
Question 4 True / False

LEAD and LAG are equivalent to window-aggregate functions like SUM OVER — they compute a value derived from most rows in the window frame.

TTrue
FFalse
Question 5 Short Answer

Why did SQL need LAG and LEAD at all? What was the alternative before window functions, and what problem does the new approach solve?

Think about your answer, then reveal below.