Skip to main content
SQL • LESSON 145

LAG

How can we access the previous order amount in chronological order?

Advanced3 Minutes1580 XP
🤔 THE QUESTION

How can we access the previous order amount in chronological order?

💡 WHAT IS IT?

LAG(column) accesses the value from the preceding row within the window without a self-join.

🎯 WHAT IS IT USED FOR?

Period-over-period delta calculation, day-over-day growth tracking, churn detection.

💻 EXAMPLE
SELECT
order_date,
amount,
LAG(amount) OVER (ORDER BY order_date) AS previous_amount
FROM orders;

🎯 Mission Objectives

Practice typing production-grade SQL code for LAG.

  • LAG() function
  • Preceding row value
  • Time-series lookback
  • Delta tracking