0
0
PostgreSQLquery~10 mins

FIRST_VALUE and LAST_VALUE in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - FIRST_VALUE and LAST_VALUE
Start with ordered data
Apply window function
FIRST_VALUE: pick first row value in window
LAST_VALUE: pick last row value in window
Return result for each row
These functions look at a set of rows ordered in a window and return the first or last value from that set for each row.
Execution Sample
PostgreSQL
SELECT id, value,
  FIRST_VALUE(value) OVER (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS first_val,
  LAST_VALUE(value) OVER (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS last_val
FROM sample_table
ORDER BY id;
This query shows how FIRST_VALUE and LAST_VALUE return the first and last 'value' in the ordered rows by 'id'.
Execution Table
StepRow idvalueWindow FrameFIRST_VALUE(value)LAST_VALUE(value)
1110[1..1]1010
2220[1..2]1020
3330[1..3]1030
4440[1..4]1040
5550[1..5]1050
💡 All rows processed; window frame moves with each row; FIRST_VALUE always first row's value; LAST_VALUE depends on frame definition.
Variable Tracker
VariableStartAfter Row 1After Row 2After Row 3After Row 4After Row 5
FIRST_VALUE(value)N/A1010101010
LAST_VALUE(value)N/A1020304050
Key Moments - 2 Insights
Why does LAST_VALUE sometimes not return the last value you expect?
Because LAST_VALUE depends on the window frame. Without specifying ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING, the frame ends at the current row, so LAST_VALUE returns the current row's value, not the last in the entire partition.
Does FIRST_VALUE change as we move through rows?
No, FIRST_VALUE always returns the first value in the window frame, which usually starts at the first row, so it stays the same for all rows.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is FIRST_VALUE(value) at row id 3?
A20
B30
C10
D40
💡 Hint
Check the FIRST_VALUE(value) column for row id 3 in the execution_table.
At which row does LAST_VALUE(value) change from 30 to 40?
ARow 2
BRow 4
CRow 3
DRow 5
💡 Hint
Look at the LAST_VALUE(value) column in the execution_table and find where it changes.
If the window frame for LAST_VALUE was not set to UNBOUNDED FOLLOWING, what would LAST_VALUE return at row 5?
A50
B40
C10
DN/A
💡 Hint
Consider that without UNBOUNDED FOLLOWING, the frame ends at the current row, so LAST_VALUE returns the current row's value.
Concept Snapshot
FIRST_VALUE(value) returns the first value in the ordered window.
LAST_VALUE(value) returns the last value in the window frame.
Window frame defaults affect LAST_VALUE results.
Use ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING for full partition.
Both functions return a value for each row in the result.
Full Transcript
This lesson shows how FIRST_VALUE and LAST_VALUE work in PostgreSQL. We start with data ordered by id. FIRST_VALUE always picks the first value in the window, so it stays the same for all rows. LAST_VALUE picks the last value in the window frame, which depends on how the frame is defined. If the frame ends at the current row, LAST_VALUE returns the current row's value. To get the last value of the entire partition, we set the frame to include all rows from the start to the end. The execution table shows these values step by step for each row. Understanding window frames is key to using LAST_VALUE correctly.