Consider a table sales with columns region, month, and revenue. The data is:
region | month | revenue -------+-------+-------- East | 1 | 100 East | 2 | 150 West | 1 | 200 West | 2 | 180
What is the output of this query?
SELECT region, month, revenue,
FIRST_VALUE(revenue) OVER (PARTITION BY region ORDER BY month) AS first_rev
FROM sales
ORDER BY region, month;SELECT region, month, revenue, FIRST_VALUE(revenue) OVER (PARTITION BY region ORDER BY month) AS first_rev FROM sales ORDER BY region, month;
Remember, FIRST_VALUE returns the first value in the window frame ordered by the specified column.
The FIRST_VALUE function returns the first revenue value for each region when ordered by month. So for East, the first revenue is 100 (month 1), and for West, it is 200 (month 1). This value repeats for all rows in the partition.
Using the same sales table as before, what is the output of this query?
SELECT region, month, revenue,
LAST_VALUE(revenue) OVER (PARTITION BY region ORDER BY month ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS last_rev
FROM sales
ORDER BY region, month;SELECT region, month, revenue, LAST_VALUE(revenue) OVER (PARTITION BY region ORDER BY month ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS last_rev FROM sales ORDER BY region, month;
LAST_VALUE needs the window frame to include all rows to get the last value in the partition.
By specifying ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING, the window frame covers all rows in the partition. So LAST_VALUE returns the last revenue per region ordered by month: 150 for East and 180 for West, repeated for all rows.
When using LAST_VALUE with an ORDER BY clause in a window function, why might it return the current row's value instead of the last value in the partition?
Think about how the window frame affects which rows are considered.
By default, the window frame for ordered window functions is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. This means LAST_VALUE only sees rows up to the current one, so it returns the current row's value, not the last in the partition. To get the true last value, you must extend the frame to include all rows.
Given a table orders with columns customer_id and order_date, which query correctly returns each order with the earliest order date for that customer?
Remember the syntax for window functions includes OVER with parentheses.
Option C correctly uses FIRST_VALUE as a window function with OVER (PARTITION BY customer_id ORDER BY order_date). Option C is invalid syntax because FIRST_VALUE requires OVER clause. Option C misses partitioning by customer. Option C has incorrect syntax missing OVER keyword.
You have a large transactions table with millions of rows, partitioned by account_id and ordered by transaction_date. You want to get the last transaction amount per account efficiently using LAST_VALUE. Which approach is best?
Think about how window frames affect performance on large data.
Using LAST_VALUE with an unbounded frame can be expensive because it processes all rows in the partition for each row. Using a subquery with GROUP BY and MAX to find the last transaction date per account, then joining back to get the amount, is often more efficient on large datasets.