SQLite LAG() Function
Example
Show each order with the date of the order before it:
SELECT OrderID, OrderDate,
LAG(OrderDate) OVER (ORDER BY OrderID)
AS PreviousDate
FROM Orders;
Try it Yourself »
Definition and Usage
The LAG() function returns a value from a row before the current row. It returns NULL, or the default value, when there is no such row.
Note: Window functions need an OVER clause. Read more in the SQLite Window Functions chapter.
Tip: Also look at LEAD().
Syntax
LAG(expression, offset, default) OVER (ORDER BY column)
Parameter Values
| Parameter | Description |
|---|---|
| expression | Required. The column or calculation to return |
| offset | Optional. How many rows back. Default is 1 |
| default | Optional. The value to return when there is no such row. Default is NULL |
Technical Details
| Return type: | Same as the input |
|---|---|
| Works in: | SQLite 3.25.0 and later |