Leadand Lag Funtion
Leadand Lag Funtion
LEAD() and LAG() are window functions in SQL that allow you to access data from subsequent or
previous rows without using self-joins or subqueries. These functions are particularly useful when
analyzing trends, changes over time, or comparing row-wise data.
Syntax
Parameters:
- column_name: The column you want to access from a different row.
- offset (optional): Number of rows ahead or behind. Default is 1.
- default_value (optional): Value to return if the LEAD or LAG row doesn’t exist.
- PARTITION BY: (Optional) Splits the data into groups.
- ORDER BY: Defines the order of rows.
LEAD(): Looking Ahead
Use Case: To compare the current row with the next row.
Explanation: This query compares an employee’s revenue with their revenue in the next month.
Useful for identifying whether sales are increasing or decreasing.
LAG(): Looking Behind
Use Case: To compare the current row with the previous row.
SELECT
employee_id,
sales_month,
revenue,
LAG(revenue) OVER (PARTITION BY employee_id ORDER BY sales_month) AS
previous_month_revenue,
revenue - LAG(revenue) OVER (PARTITION BY employee_id ORDER BY sales_month) AS
Explanation: This helps identify how much revenue has changed compared to the previous month.
Scenario-Based Examples
Scenario 1: Identify First-Time Purchase
SELECT
customer_id,
purchase_date,
LAG(purchase_date) OVER (PARTITION BY customer_id ORDER BY purchase_date) AS
previous_purchase,
CASE
WHEN LAG(purchase_date) OVER (PARTITION BY customer_id ORDER BY
purchase_date) IS NULL THEN 'First Purchase' ELSE 'Repeat Purchase' END
AS purchase_type FROM purchases;
SELECT
product_id,
price_date,
price,
LAG(price) OVER (PARTITION BY product_id ORDER BY price_date) AS previous_price,
CASE
WHEN price < LAG(price) OVER (PARTITION BY product_id ORDER BY price_date)
THEN 'Price Dropped' ELSE 'No Drop' END AS price_trend FROM
product_prices;
SELECT
patient_id,
admission_date,
discharge_date,
LEAD(admission_date) OVER (PARTITION BY patient_id ORDER BY admission_date) AS
next_admission,
CASE
WHEN DATEDIFF(
DAY, discharge_date,
LEAD(admission_date) OVER (PARTITION BY patient_id ORDER BY
admission_date)
) <= 30 THEN 'Readmitted Within 30 Days' ELSE 'No Readmission'
END AS readmission_status FROM admissions;
Pro Tips
- Combine with CASE WHEN for flag-based logic.
- Use with PARTITION BY to ensure correct grouping (e.g., by customer, product, or employee).
- Chain with DATEDIFF or arithmetic operators for powerful comparisons.