diff --git a/content/sql/concepts/window-functions/terms/lag/lag.md b/content/sql/concepts/window-functions/terms/lag/lag.md index a1ebf767cba..50c0e56f1fd 100644 --- a/content/sql/concepts/window-functions/terms/lag/lag.md +++ b/content/sql/concepts/window-functions/terms/lag/lag.md @@ -43,19 +43,63 @@ Users Table | kyle | xy | 60 | ```sql -SELECT *, +SELECT + first_name, + last_name, + age, LAG(age, 1) OVER ( - ORDER BY age ASC) AS previous_age + ORDER BY age DESC + ) AS previous_age FROM Users; ``` -The output is a table that features a new column `previous_age`, which holds the values from the previous records. The first record is null because a default was not specified and the previous row would be out of range. - -Output +The output of the above code is a table that features a new column `previous_age`, which holds the values from the previous records. The first record is null because a default was not specified and the previous row would be out of range. | first_name | last_name | age | previous_age | | ---------- | --------- | --- | ------------ | -| kyle | xy | 60 | null | +| kyle | xy | 60 | NULL | | jenna | black | 35 | 60 | | chris | smith | 30 | 35 | | dave | james | 19 | 30 | + +### Using `PARTITION BY` Clause + +This example demonstrates how to use the `LAG()` function to create a new column, `previous_position`. + +The `PARTITION BY employee_id` clause ensures that the `LAG()` function operates within each group of rows that share the same `employee_id`. The `ORDER BY promotion_date` ensures the rows are processed in chronological order. + +`Promotions` Table + +| employee_id | promotion_date | new_position | +| ----------- | -------------- | ------------ | +| 1 | 2020-01-01 | Junior Dev | +| 1 | 2021-06-01 | Mid Dev | +| 1 | 2024-03-01 | Senior Dev | +| 2 | 2019-05-01 | Intern | +| 2 | 2022-11-01 | Analyst | +| 2 | 2024-11-20 | Data Analyst | + +```sql +SELECT + employee_id, + promotion_date, + new_position, + LAG(new_position) OVER ( + PARTITION BY employee_id + ORDER BY promotion_date + ) AS previous_position +FROM Promotions; +``` + +Within each group defined by `employee_id`, the `previous_position` column holds the value from the previous row based on `promotion_date`. The first record in each group is `NULL` because there is no preceding row. + +The above code generates the following output: + +| employee_id | promotion_date | new_position | previous_position | +| ----------- | -------------- | ------------ | ----------------- | +| 1 | 2020-01-01 | Junior Dev | NULL | +| 1 | 2021-06-01 | Mid Dev | Junior Dev | +| 1 | 2024-03-01 | Senior Dev | Mid Dev | +| 2 | 2019-05-01 | Intern | NULL | +| 2 | 2022-11-01 | Analyst | Intern | +| 2 | 2024-11-20 | Data Analyst | Analyst |