Challenge - 5 Problems
LAG and LEAD Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of LAG function with partition
Given the table sales with columns
region, month, and revenue, what is the output of the following query?SELECT region, month, revenue, LAG(revenue) OVER (PARTITION BY region ORDER BY month) AS prev_revenue FROM sales ORDER BY region, month;
PostgreSQL
CREATE TABLE sales (region TEXT, month INT, revenue INT); INSERT INTO sales VALUES ('East', 1, 100), ('East', 2, 150), ('West', 1, 200), ('West', 2, 180); SELECT region, month, revenue, LAG(revenue) OVER (PARTITION BY region ORDER BY month) AS prev_revenue FROM sales ORDER BY region, month;
Attempts:
2 left
💡 Hint
Think about how LAG works with partition and order.
✗ Incorrect
LAG returns the previous row's value within the same partition ordered by month. For the first month in each region, there is no previous row, so it returns null.
❓ query_result
intermediate2:00remaining
Output of LEAD function without partition
Consider the table employees with columns
id, name, and salary. What is the output of this query?SELECT id, name, salary, LEAD(salary) OVER (ORDER BY id) AS next_salary FROM employees ORDER BY id;
PostgreSQL
CREATE TABLE employees (id INT, name TEXT, salary INT); INSERT INTO employees VALUES (1, 'Alice', 5000), (2, 'Bob', 6000), (3, 'Charlie', 5500); SELECT id, name, salary, LEAD(salary) OVER (ORDER BY id) AS next_salary FROM employees ORDER BY id;
Attempts:
2 left
💡 Hint
LEAD returns the next row's value ordered by id.
✗ Incorrect
LEAD(salary) returns the salary of the next employee ordered by id. The last row has no next row, so it returns null.
📝 Syntax
advanced2:00remaining
Identify the syntax error in LAG usage
Which option contains a syntax error when using the LAG function in PostgreSQL?
PostgreSQL
SELECT id, value, LAG(value, 2, 0) OVER (ORDER BY id) AS lag_val FROM data;
Attempts:
2 left
💡 Hint
Check the commas between function arguments.
✗ Incorrect
Option B misses commas between arguments in LAG function, causing a syntax error.
❓ optimization
advanced2:00remaining
Optimizing LEAD usage for large datasets
You have a large table orders with columns
order_id, customer_id, and order_date. You want to find the next order date for each customer efficiently. Which query is the most optimized?Attempts:
2 left
💡 Hint
Window functions are usually faster than correlated subqueries.
✗ Incorrect
Option A uses LEAD with partition and order, which is efficient for large datasets. Option A uses a correlated subquery which is slower. Option A does not partition by customer, so results are incorrect. Option A uses MAX which does not find the next order date.
🧠 Conceptual
expert3:00remaining
Understanding NULL handling in LAG and LEAD
Consider a table temps with columns
Specifically, what happens when the previous row's temperature is NULL?
day and temperature. Some temperature values are NULL. What will be the result of this query?SELECT day, temperature, LAG(temperature, 1, 20) OVER (ORDER BY day) AS prev_temp FROM temps ORDER BY day;
Specifically, what happens when the previous row's temperature is NULL?
Attempts:
2 left
💡 Hint
Default value is used only when there is no previous row, not when previous value is NULL.
✗ Incorrect
LAG returns the previous row's value as is, including NULLs. The default value is used only if there is no previous row.