How to Get Next Row Value in Oracle?

How to Get Next Row Value in Oracle?

  • SQL
  • 1 min read

Use the lead() function to get the next row value in Oracle. Below are its syntax and example:

Lead Function Syntax

lead(expr, offset, default_value) over (partition_by_clause order_by_clause)

For the expr, specify the column name, for offset, specify the row number, and for the default_value, specify the value to show the default value for the last row. Also, specify the partition or order by clause for the window function.

Oracle SQL Query Example to Get Next Row Value

The following SQL query will get the data from the EMP table and will display the next row salary in the current row:

select ename, job, sal, 
       lead(sal, 1, 0) over (order by ename) next_salary
   from emp;

Output:

ENAMEJOBSALNEXT_SALARY
ADAMSCLERK11001600
ALLENSALESMAN16002850
BLAKEMANAGER28502450
CLARKMANAGER24503000
FORDANALYST3000950
JAMESCLERK9502975
JONESMANAGER29755000
KINGPRESIDENT50001250
MARTINSALESMAN12501300
MILLERCLERK13003000
SCOTTANALYST3000800
SMITHCLERK8001500
TURNERSALESMAN15001250
WARDSALESMAN12500

See also: