Challenge - 5 Problems
Window Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of FIRST_VALUE with window frame
Given the table sales with columns
region, month, and revenue, what is the output of the following query?SELECT region, month, revenue,
FIRST_VALUE(revenue) OVER (PARTITION BY region ORDER BY month ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS first_rev
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), ('East', 3, 120), ('West', 1, 200), ('West', 2, 180); SELECT region, month, revenue, FIRST_VALUE(revenue) OVER (PARTITION BY region ORDER BY month ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS first_rev FROM sales ORDER BY region, month;
Attempts:
2 left
💡 Hint
Remember that FIRST_VALUE returns the first value in the window frame defined by the ORDER BY and ROWS clause.
✗ Incorrect
The window frame is from the start (UNBOUNDED PRECEDING) to the current row, so FIRST_VALUE always returns the revenue of the first month in each region partition.
❓ query_result
intermediate2:00remaining
Output of LAST_VALUE with default frame
Consider the table orders with columns
customer, order_date, and amount. What is the output of this query?SELECT customer, order_date, amount,
LAST_VALUE(amount) OVER (PARTITION BY customer ORDER BY order_date) AS last_amt
FROM orders
ORDER BY customer, order_date;
PostgreSQL
CREATE TABLE orders (customer TEXT, order_date DATE, amount INT); INSERT INTO orders VALUES ('Alice', '2024-01-01', 50), ('Alice', '2024-01-05', 70), ('Bob', '2024-01-02', 30), ('Bob', '2024-01-03', 40); SELECT customer, order_date, amount, LAST_VALUE(amount) OVER (PARTITION BY customer ORDER BY order_date) AS last_amt FROM orders ORDER BY customer, order_date;
Attempts:
2 left
💡 Hint
LAST_VALUE by default uses the window frame from the first row to the current row, so it returns the last value in that frame, which is the current row's value.
✗ Incorrect
Without specifying ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING, LAST_VALUE returns the value of the current row, not the last in the partition.
📝 Syntax
advanced2:00remaining
Identify the syntax error in FIRST_VALUE usage
Which option contains a syntax error when using FIRST_VALUE in PostgreSQL?
Attempts:
2 left
💡 Hint
Remember that FIRST_VALUE is a window function and requires an OVER clause.
✗ Incorrect
Option B lacks the OVER clause, so it is a syntax error. FIRST_VALUE must be used with OVER to define the window.
❓ optimization
advanced2:00remaining
Optimizing LAST_VALUE for full partition
You want to get the last sale amount per customer over all their orders. Which query correctly uses LAST_VALUE to always return the last amount in the entire partition regardless of row position?
Attempts:
2 left
💡 Hint
To get the last value in the entire partition, the window frame must cover all rows.
✗ Incorrect
Option C sets the frame from the first to the last row in the partition, so LAST_VALUE returns the last amount overall.
🧠 Conceptual
expert3:00remaining
Understanding window frame impact on FIRST_VALUE and LAST_VALUE
Which statement best explains why the output of FIRST_VALUE and LAST_VALUE can differ depending on the window frame specified?
Attempts:
2 left
💡 Hint
Think about how the window frame limits the rows the function looks at for each row.
✗ Incorrect
The window frame restricts the rows considered for each row, so FIRST_VALUE and LAST_VALUE return values based on that subset, not the entire partition.