0
0
SQLquery~10 mins

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

Choose your learning style9 modes available
Concept Flow - FIRST_VALUE and LAST_VALUE
Start Query
Partition Data (optional)
Order Rows within Partition
Apply FIRST_VALUE/LAST_VALUE Window Function
Return Result Set with New Columns
End Query
The query starts by optionally partitioning data, then ordering rows, applying FIRST_VALUE or LAST_VALUE to get first or last values in the window, and returns the result.
Execution Sample
SQL
SELECT
  department,
  employee,
  salary,
  FIRST_VALUE(salary) OVER (PARTITION BY department ORDER BY salary DESC) AS highest_salary,
  LAST_VALUE(salary) OVER (PARTITION BY department ORDER BY salary DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS lowest_salary
FROM employees;
This query shows the highest and lowest salary per department using FIRST_VALUE and LAST_VALUE window functions.
Execution Table
StepRow (department, employee, salary)Window OrderFIRST_VALUE(salary)LAST_VALUE(salary)Notes
1(Sales, Alice, 9000)9000, 7000, 600090006000First row in Sales partition, highest salary is 9000, last salary is 6000
2(Sales, Bob, 7000)9000, 7000, 600090006000Second row, FIRST_VALUE stays 9000, LAST_VALUE includes full window with frame clause
3(Sales, Carol, 6000)9000, 7000, 600090006000Third row, same FIRST_VALUE and LAST_VALUE
4(HR, Dave, 8000)8000, 750080007500First row in HR, highest salary 8000, lowest 7500
5(HR, Eve, 7500)8000, 750080007500Second row, values remain same
6End of rowsQuery completes after all rows processed
💡 All rows processed, window functions applied per partition and order.
Variable Tracker
VariableStartAfter Row 1After Row 2After Row 3After Row 4After Row 5Final
departmentN/ASalesSalesSalesHRHRN/A
employeeN/AAliceBobCarolDaveEveN/A
salaryN/A90007000600080007500N/A
FIRST_VALUE(salary)N/A90009000900080008000N/A
LAST_VALUE(salary)N/A60006000600075007500N/A
Key Moments - 2 Insights
Why does LAST_VALUE need the frame clause 'ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING' to return the expected lowest salary?
Without the frame clause, LAST_VALUE defaults to the frame from the start of the partition to the current row, which can cause it to return the current row's value instead of the last in the full partition. The frame clause ensures the window covers all rows.
Why does FIRST_VALUE always return the same highest salary for all rows in the partition?
Because FIRST_VALUE returns the first value in the ordered window, and since the order is descending by salary, the first value is the highest salary, which stays the same for all rows in that partition.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the FIRST_VALUE(salary) for the second row in the Sales department?
A6000
B7000
C9000
DNULL
💡 Hint
Check the row 2 in the execution_table under FIRST_VALUE(salary)
At which step does the LAST_VALUE(salary) for HR department first appear as 7500?
AStep 4
BStep 5
CStep 3
DStep 2
💡 Hint
Look at the LAST_VALUE(salary) column for HR rows in execution_table
If we remove the frame clause from LAST_VALUE, what would likely happen to the LAST_VALUE(salary) output?
AIt would always show the lowest salary
BIt would show the salary of the current row
CIt would cause a syntax error
DIt would show NULL
💡 Hint
Refer to key_moments about the importance of the frame clause for LAST_VALUE
Concept Snapshot
FIRST_VALUE and LAST_VALUE are window functions.
They return the first or last value in an ordered window.
Use PARTITION BY to group rows.
Use ORDER BY to define order.
LAST_VALUE often needs a frame clause to cover full partition.
They help find first/last values without aggregation.
Full Transcript
This visual execution trace shows how FIRST_VALUE and LAST_VALUE window functions work in SQL. The query partitions employees by department and orders them by salary descending. FIRST_VALUE returns the highest salary in each department for every row. LAST_VALUE returns the lowest salary, but requires a frame clause to cover the entire partition; otherwise, it may return the current row's salary. The execution table walks through each row, showing how these values are computed and remain consistent across rows in the same partition. Key moments clarify why the frame clause is needed for LAST_VALUE and why FIRST_VALUE stays constant. The quiz tests understanding of these behaviors by referencing specific steps and values in the execution table. The snapshot summarizes the syntax and key rules for these window functions.