Site icon Radu Pârvu

Oracle LEAD: Simplifying SQL Queries Made Easy

Introduction

In this post, we will continue the series of unused Oracle Database features.

The Oracle LEAD analytic function allows you to access data from the next row in a result set without using a self-join.
It is extremely useful for:
• comparing current and next rows,
• trend analysis,
• finding changes between records,
• calculating differences,
• and simplifying SQL queries.
In this article, we will explore practical examples of the Oracle LEAD function.

What is Oracle LEAD?

The LEAD function returns data from a subsequent row based on a specified offset.

Basic Syntax

LEAD(column_name, offset, default_value)
OVER (
PARTITION BY column_name
ORDER BY column_name
)

Parameters:

Examples

Create Sample Table

Let us create a simple employees table. I name it with suffix 2 to not confuse it with same table example used in other articles from Parvu.org:

CREATE TABLE employees2 (
employee_id NUMBER,
employee_name VARCHAR2(100),
department_id NUMBER,
salary NUMBER
);

Insert sample data

INSERT INTO employees2 VALUES (1, 'John', 10, 5000);
INSERT INTO employees2 VALUES (2, 'Emma', 10, 6500);
INSERT INTO employees2 VALUES (3, 'Liam', 10, 7200);
INSERT INTO employees2 VALUES (4, 'Olivia', 20, 4500);
INSERT INTO employees2 VALUES (5, 'Noah', 20, 5200);
COMMIT;

The table data is :

EMPLOYEE_ID EMPLOYEE_NAME DEPARTMENT_ID SALARY
----------- ---------------------------------------------------------------------------------------------------- ------------- ----------
1 John 10 5000
2 Emma 10 6500
3 Liam 10 7200
4 Olivia 20 4500
5 Noah 20 5200

Basic LEAD Example

The following query retrieves the next employee salary based on employee ID:

SELECT employee_id,
employee_name,
salary,
LEAD(salary)
OVER (ORDER BY employee_id) AS next_salary
FROM employees2;
Result
EMPLOYEE_ID EMPLOYEE_NAME SALARY NEXT_SALARY
----------- ---------------------------------------------------------------------------------------------------- ---------- -----------
1 John 5000 6500
2 Emma 6500 7200
3 Liam 7200 4500
4 Olivia 4500 5200
5 Noah 5200
EMPLOYEE_IDEMPLOYEE_NAMESALARYNEXT_SALARY
1John50006500
2Emma65007200
3Liam72004500
4Olivia45005200
5Noah5200NULL

The LEAD function reads the value from the next row.

LEAD with PARTITION BY

You can restart calculations within groups!

The following example retrieves the next salary within each department.

SELECT employee_name,
department_id,
salary,
LEAD(salary)
OVER (
PARTITION BY department_id
ORDER BY salary
) AS next_salary
FROM employees2;
Output:
EMPLOYEE_NAME DEPARTMENT_ID SALARY NEXT_SALARY
---------------------------------------------------------------------------------------------------- ------------- ---------- -----------
John 10 5000 6500
Emma 10 6500 7200
Liam 10 7200
Olivia 20 4500 5200
Noah 20 5200
Explanation

LEAD with Offset

The second parameter defines how many rows forward to look:

SELECT employee_name,
salary,
LEAD(salary, 2)
OVER (ORDER BY employee_id) AS salary_after_next
FROM employees2;

This retrieves the salary two rows ahead:

EMPLOYEE_NAME SALARY SALARY_AFTER_NEXT
---------------------------------------------------------------------------------------------------- ---------- -----------------
John 5000 7200
Emma 6500 4500
Liam 7200 5200
Olivia 4500
Noah 5200

LEAD with Default Value

Instead of returning NULL, you can define a custom value:

SELECT employee_name,
salary,
LEAD(salary, 1, 0)
OVER (ORDER BY employee_id) AS next_salary
FROM employees2;

If no next row exists, Oracle returns 0.

Output
EMPLOYEE_NAME SALARY NEXT_SALARY
---------------------------------------------------------------------------------------------------- ---------- -----------
John 5000 6500
Emma 6500 7200
Liam 7200 4500
Olivia 4500 5200
Noah 5200 0

Calculate Salary Difference Using LEAD

One of the most practical uses of LEAD is calculating differences between rows.

SELECT employee_name,
salary,
LEAD(salary)
OVER (ORDER BY employee_id) AS next_salary,
LEAD(salary)
OVER (ORDER BY employee_id) - salary AS salary_difference
FROM employees2;

Output

EMPLOYEE_NAME SALARY NEXT_SALARY SALARY_DIFFERENCE
---------------------------------------------------------------------------------------------------- ---------- ----------- -----------------
John 5000 6500 1500
Emma 6500 7200 700
Liam 7200 4500 -2700
Olivia 4500 5200 700
Noah 5200

Use case

This application This Is useful for:

Common Use Cases

Oracle LEAD is commonly used for:


• comparing consecutive rows,
• identifying value changes,
• trend analysis,
• reporting,
• and time-based calculations.

Conclusion

The Oracle LEAD analytic function is a powerful feature that allows you to access future rows without complex joins.
Key advantages:
• simpler SQL,
• better readability,
• powerful analytical capabilities,
• and efficient reporting queries.
When working with sequential or grouped data, LEAD can significantly simplify your SQL statements.

Exit mobile version