0
0
SQLquery~20 mins

FIRST_VALUE and LAST_VALUE in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Window Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of FIRST_VALUE in this query?

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;
SQL
SELECT region, month, revenue,
       FIRST_VALUE(revenue) OVER (PARTITION BY region ORDER BY month) AS first_rev
FROM sales
ORDER BY region, month;
A[{"region": "East", "month": 1, "revenue": 100, "first_rev": 100}, {"region": "East", "month": 2, "revenue": 150, "first_rev": 150}, {"region": "West", "month": 1, "revenue": 200, "first_rev": 200}, {"region": "West", "month": 2, "revenue": 180, "first_rev": 180}]
B[{"region": "East", "month": 1, "revenue": 100, "first_rev": null}, {"region": "East", "month": 2, "revenue": 150, "first_rev": null}, {"region": "West", "month": 1, "revenue": 200, "first_rev": null}, {"region": "West", "month": 2, "revenue": 180, "first_rev": null}]
C[{"region": "East", "month": 1, "revenue": 100, "first_rev": 150}, {"region": "East", "month": 2, "revenue": 150, "first_rev": 150}, {"region": "West", "month": 1, "revenue": 200, "first_rev": 180}, {"region": "West", "month": 2, "revenue": 180, "first_rev": 180}]
D[{"region": "East", "month": 1, "revenue": 100, "first_rev": 100}, {"region": "East", "month": 2, "revenue": 150, "first_rev": 100}, {"region": "West", "month": 1, "revenue": 200, "first_rev": 200}, {"region": "West", "month": 2, "revenue": 180, "first_rev": 200}]
Attempts:
2 left
💡 Hint

Remember, FIRST_VALUE returns the first value in the window frame ordered by the specified column.

query_result
intermediate
2:00remaining
What does LAST_VALUE return in this query?

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;
SQL
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;
A[{"region": "East", "month": 1, "revenue": 100, "last_rev": 150}, {"region": "East", "month": 2, "revenue": 150, "last_rev": 150}, {"region": "West", "month": 1, "revenue": 200, "last_rev": 180}, {"region": "West", "month": 2, "revenue": 180, "last_rev": 180}]
B[{"region": "East", "month": 1, "revenue": 100, "last_rev": 150}, {"region": "East", "month": 2, "revenue": 150, "last_rev": 100}, {"region": "West", "month": 1, "revenue": 200, "last_rev": 180}, {"region": "West", "month": 2, "revenue": 180, "last_rev": 200}]
C[{"region": "East", "month": 1, "revenue": 100, "last_rev": 100}, {"region": "East", "month": 2, "revenue": 150, "last_rev": 150}, {"region": "West", "month": 1, "revenue": 200, "last_rev": 200}, {"region": "West", "month": 2, "revenue": 180, "last_rev": 180}]
D[{"region": "East", "month": 1, "revenue": 100, "last_rev": null}, {"region": "East", "month": 2, "revenue": 150, "last_rev": null}, {"region": "West", "month": 1, "revenue": 200, "last_rev": null}, {"region": "West", "month": 2, "revenue": 180, "last_rev": null}]
Attempts:
2 left
💡 Hint

LAST_VALUE needs the window frame to include all rows to get the last value in the partition.

🧠 Conceptual
advanced
2:00remaining
Why does LAST_VALUE sometimes return unexpected results?

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?

ABecause the default window frame ends at the current row, so LAST_VALUE returns the value of the current row, not the last in the partition.
BBecause LAST_VALUE always ignores the ORDER BY clause and returns the first value.
CBecause the partitioning is ignored when using LAST_VALUE, so it returns random values.
DBecause the function requires an explicit GROUP BY clause to work correctly.
Attempts:
2 left
💡 Hint

Think about how the window frame affects which rows are considered.

📝 Syntax
advanced
2:00remaining
Which query correctly uses FIRST_VALUE to get the earliest date per customer?

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?

ASELECT customer_id, order_date, FIRST_VALUE(order_date) PARTITION BY customer_id ORDER BY order_date FROM orders;
BSELECT customer_id, order_date, FIRST_VALUE(order_date) FROM orders GROUP BY customer_id ORDER BY order_date;
CSELECT customer_id, order_date, FIRST_VALUE(order_date) OVER (PARTITION BY customer_id ORDER BY order_date) AS first_order FROM orders;
DSELECT customer_id, order_date, FIRST_VALUE(order_date) OVER (ORDER BY customer_id) AS first_order FROM orders;
Attempts:
2 left
💡 Hint

Remember the syntax for window functions includes OVER with parentheses.

optimization
expert
3:00remaining
Optimizing LAST_VALUE usage for large datasets

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?

AUse LAST_VALUE without specifying window frame, relying on default frame for performance.
BUse a subquery with GROUP BY account_id and MAX(transaction_date) joined back to get last transaction amount.
CUse LAST_VALUE with window frame <code>ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING</code> to get the last value per partition.
DUse FIRST_VALUE ordered by transaction_date descending with default window frame.
Attempts:
2 left
💡 Hint

Think about how window frames affect performance on large data.