This Oracle tutorial explains how to use the Oracle/PLSQL LAST_VALUE function with syntax and examples.
The Oracle/PLSQL LAST_VALUE function returns the last value in an ordered set of values from an analytic window. It is similar to the FIRST_VALUE and NTH_VALUE functions.
The ANSI compatible syntax for the LAST_VALUE function in Oracle/PLSQL is:
The following syntax is also an accepted format:
The column or expression that you wish to return the last value for.
Optional. It determines whether NULL values are included or ignored in the analytic window calculation. If this parameter is omitted, the default is RESPECT NULLS which includes NULL values.
Optional. It is used to partition the results into groups based on one or more expressions.
Optional. It is used to order the data within each partition.
Optional. It determines the rows in the analytic window to evaluate and it is important that you use the correct windowing_clause or you could get unexpected results. It can be a value such as:
windowing_clause | Description |
---|---|
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW | Last row in the window changes as the current row changes (default) |
RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING | First row in the window changes as the current row changes |
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING | All rows are included in the window, regardless of the current row |
The LAST_VALUE function returns the last value in an ordered set of values from an analytic window.
The LAST_VALUE function can be used in the following versions of Oracle/PLSQL:
If you want to follow along with this tutorial, get the DDL to create the tables and the DML to populate the data. Then try the examples in your own database!
Let's look at some Oracle LAST_VALUE function examples and explore how to use the LAST_VALUE function in Oracle/PLSQL.
Let's start with a simple example and use the LAST_VALUE function to return the highest salary in the employees table. In this example, we won't require a query_partition_clause because we are evaluating across the entire employees table.
In this example, we have a table called employees with the following data:
To find the highest salary, enter the following SELECT statement:
These are the results that you should see:
In this example, the LAST_VALUE returns the highest salary value as specified by LAST_VALUE(salary). The analytic window will sort the data by salary in ascending order as specified by ORDER BY salary ASC. The RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING windowing_clause is used to ensure that all rows are included regardless of the current row.
And because we want the highest salary for the table, we did not need to include a query_partition_clause to partition the data.
Now, let's show you how to use the LAST_VALUE function with a query_partition_clause. In this next example, let's return the highest salary for dept_id 10 and 20.
Based on the same employees table, enter the following SQL statement:
These are the results that you should see:
In this example, the LAST_VALUE returns the highest salary value as specified by LAST_VALUE(salary). The analytic window will partition results by dept_id and order the data by salary in ascending order as specified by PARTITION BY dept_id ORDER BY salary ASC.
Now, let's show you how to use the LAST_VALUE function to return the lowest salary for dept_id 10 and 20.
Again based on the data in the employees table, enter the following SQL statement:
These are the results that you should see:
In this example, we have switched the sort order to descending on the partition as specified by PARTITION BY dept_id ORDER BY salary DESC and now we get the lowest salary based on dept_id.