Accesses a value from a previous row in the partition based on the specified offset.
Same as input expressionDiperbarui: 13 Jun 2026LAG(expression [, offset [, default_value]]) OVER ([PARTITION BY partition_expression] ORDER BY sort_expression)Column or expression whose value is retrieved
Determines the row ordering
Number of rows to look back (default: 1)
Default: 1
Value returned when no previous row exists
1 SELECT 2 month, 3 revenue, 4 LAG(revenue) OVER (ORDER BY month) as prev_month_revenue, 5 revenue - LAG(revenue) OVER (ORDER BY month) as growth 6 FROM `project.dataset.monthly_sales`;
Calculates monthly growth by comparing revenue to the previous month.
| month | revenue | prev_month | growth |
|---|---|---|---|
| 2024-01 | 100000 | NULL | NULL |
| 2024-02 | 120000 | 100000 | 20000 |
| 2024-03 | 115000 | 120000 | -5000 |
1 SELECT 2 date, 3 stock_price, 4 LAG(stock_price, 1, stock_price) OVER (ORDER BY date) as prev_price 5 FROM `project.dataset.stocks`;
Uses the current price as the default value for the first row.
Sudah paham LAG? Latih langsung di browser
Latihan interaktif, langsung di browser.