# LAG() Function

### Description

Provides access to data from the **previous row** in the result set without needing a self-join

### Syntax

```sql
LAG(expression [, offset [, default]]) OVER ([PARTITION BY partition_expression] ORDER BY sort_expression)
```

### Parameters

| Parameter | Description | Data Type | Required |
| --- | --- | --- | --- |
| _expression_ | Column or expression whose value is returned from an earlier row | Any | Yes |
| _offset_ | Number of rows to look backward (defaults to 1) | Integer | No |
| _default_ | Value returned when the target row is outside the partition | Any | No |

### Rewrite

Use QUALIFY to filter on window expression results; do not use window functions in WHERE.

#### Notes

-   Include an `OVER (...)` clause with `ORDER BY` (and optional `PARTITION BY`) when using this as a window function.

### Example

```sql
SELECT
    id, LAG(salary, 1) OVER (PARTITION BY department ORDER BY start_date) AS prev_salary
FROM
    rippling.employee.Employee;
```

Source: https://developer.rippling.com/documentation/rippling-platform/transformations/sql-reference/lag

> For the complete documentation index, see [llms.txt](https://developer.rippling.com/llms.txt). Public pages have a Markdown version at the same URL with .md added.
