Member-only story
SQL: LAG and LEAD Functions
We will examine the LAG and LEAD functions to effectively conclude our series on Window Functions.
READ FOR FREE
TABLE OF CONTENTS
· INTRODUCTION
· LAG AND LEAD
· FUNÇÃO LAG
· LEAD
· CONCLUSION
INTRODUCTION
These analytical functions within SQL act as offsets for query results.
These functions evaluate the current line, displaying the previous value (LAG) and the next value (LEAD).
They are based on the ORDER BY clause and use the column specified by the partition to return the desired value.
It accepts three parameters as arguments:
- The column we want for comparison to return the value.
- The number of rows returned will be specified, with one row being the default return value.
- When no value is specified, the default return value is NULL.
LAG FUNCTION.
In the first example, the PRE_VAL column displays the value from the previous row, starting at the second line (column RNUMBER).










