0
0
SQLquery~10 mins

LAG function for previous row access in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - LAG function for previous row access
Start with ordered rows
For each row
Look at previous row in order
Return previous row's value
If no previous row, return NULL or default
Output current row with previous value
Repeat for all rows
End
The LAG function looks at the previous row's value in a defined order and returns it for the current row, or NULL if there is no previous row.
Execution Sample
SQL
SELECT id, sales, LAG(sales) OVER (ORDER BY id) AS prev_sales
FROM sales_data;
This query returns each row's sales and the sales value from the previous row ordered by id.
Execution Table
StepCurrent Row (id)Current salesPrevious Row sales (LAG)Output Row (id, sales, prev_sales)
11100NULL(1, 100, NULL)
22150100(2, 150, 100)
33200150(3, 200, 150)
44130200(4, 130, 200)
55170130(5, 170, 130)
6End--No more rows, stop
💡 Reached end of rows; no more previous rows to access.
Variable Tracker
VariableStartAfter Row 1After Row 2After Row 3After Row 4After Row 5Final
Current Row idN/A12345End
Current salesN/A100150200130170N/A
LAG(sales)N/ANULL100150200130N/A
Key Moments - 3 Insights
Why does the first row's LAG value return NULL?
Because there is no previous row before the first row, the LAG function returns NULL as shown in execution_table step 1.
How does the LAG function know which row is previous?
It uses the ORDER BY clause inside the OVER() clause to define the order, so the previous row is the one with the immediately smaller id, as seen in the execution_table.
What happens if the order is changed?
The previous row changes according to the new order, so the LAG values will reflect the previous row in that new order, changing the output rows accordingly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the LAG(sales) value at step 3?
A200
B150
C100
DNULL
💡 Hint
Check the 'Previous Row sales (LAG)' column at step 3 in the execution_table.
At which step does the LAG function first return a non-NULL value?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Previous Row sales (LAG)' column in execution_table for the first non-NULL value.
If the ORDER BY was changed to ORDER BY sales DESC, how would the LAG value for id=2 change?
AIt would be 170
BIt would be NULL
CIt would be 150
DIt would be 100
💡 Hint
Think about the new order by sales descending and which row comes before id=2 in that order.
Concept Snapshot
LAG(value) OVER (ORDER BY column) returns the previous row's value in the specified order.
If no previous row exists, it returns NULL by default.
Useful for comparing current row with previous row in ordered data.
ORDER BY inside OVER() defines the row sequence.
Can specify default value as third argument to LAG.
Helps analyze trends or differences between rows.
Full Transcript
The LAG function in SQL lets you access the value from the previous row in a result set ordered by a column. It works by looking at each row in order and returning the value from the row before it. If there is no previous row, it returns NULL. For example, if you have sales data ordered by id, LAG(sales) will give you the sales from the previous id. This is useful to compare current and previous values easily. The order is important because it defines which row is considered previous. Changing the order changes the LAG results. Beginners often wonder why the first row returns NULL and how the previous row is determined. The execution table shows step by step how each row's previous value is found. This helps understand the flow and output clearly.