Consider a sales table with columns region, month, and sales. What is the output of the following query?
SELECT region, month, sales, LAG(sales) OVER (PARTITION BY region ORDER BY month) AS prev_sales FROM sales ORDER BY region, month;
CREATE TABLE sales (region VARCHAR(10), month INT, sales INT); INSERT INTO sales VALUES ('East', 1, 100), ('East', 2, 150), ('West', 1, 200), ('West', 2, 180); SELECT region, month, sales, LAG(sales) OVER (PARTITION BY region ORDER BY month) AS prev_sales FROM sales ORDER BY region, month;
Remember that LAG returns the value from the previous row within the same partition ordered by the specified column.
The LAG function returns the previous row's sales value within each region partition ordered by month. For the first month in each region, there is no previous row, so it returns null.
What will be the output of the prev_sales column in the following query if the default value is set to 0?
SELECT month, sales, LAG(sales, 1, 0) OVER (ORDER BY month) AS prev_sales FROM sales ORDER BY month;
CREATE TABLE sales (month INT, sales INT); INSERT INTO sales VALUES (1, 100), (2, 150), (3, 200); SELECT month, sales, LAG(sales, 1, 0) OVER (ORDER BY month) AS prev_sales FROM sales ORDER BY month;
The third argument in LAG is the default value used when there is no previous row.
Since the first row has no previous row, LAG returns the default value 0 instead of null.
Which option contains a syntax error in the use of the LAG function?
SELECT id, value, LAG(value 1) OVER (ORDER BY id) AS prev_val FROM data;
CREATE TABLE data (id INT, value INT); INSERT INTO data VALUES (1, 10), (2, 20); SELECT id, value, LAG(value 1) OVER (ORDER BY id) AS prev_val FROM data;
Check the commas between function arguments.
Option C is missing a comma between value and 1, causing a syntax error.
You have a large table with millions of rows. Which approach is best to optimize the performance of a query using LAG?
Think about how window functions process data partitions.
Using PARTITION BY and ORDER BY properly limits the rows each window function processes, improving performance.
A query uses LAG(sales) OVER (ORDER BY month) but unexpectedly returns NULL for some rows that are not the first row. What is the most likely cause?
Consider how ordering affects which row is considered previous.
If month has duplicates, the order of rows with the same month is not guaranteed, so LAG may return NULL unexpectedly.