← Back to SQL, page 6

sql-0118 · Medium mid

How do you compare each day's sales with the previous day's? Explain LAG and LEAD.

LAG(expr) returns the value from the previous row in the window order and LEAD(expr) from the next row. LAG(amount) OVER (ORDER BY day) gives yesterday's amount, so amount - LAG(amount) is the change. Both take an offset and a default: LAG(amount, 1, 0) returns 0 instead of NULL for the first row.

Full answer
sql
CREATE TABLE daily_sales (
  day    TEXT PRIMARY KEY,
  amount INTEGER NOT NULL
);
INSERT INTO daily_sales VALUES
  ('2026-03-01',120),('2026-03-02',80),('2026-03-03',150),
  ('2026-03-05',90),('2026-03-06',60),('2026-03-09',200);
sql
SELECT day, amount,
       LAG(amount) OVER (ORDER BY day)          AS prev_amount,
       amount - LAG(amount) OVER (ORDER BY day) AS change
FROM daily_sales
ORDER BY day;
dayamountprev_amountchange
2026-03-01120NULLNULL
2026-03-0280120-40
2026-03-031508070
2026-03-0590150-60
2026-03-066090-30
2026-03-0920060140

The first row has no previous row, so LAG returns NULL, and the change is NULL too. That is usually more honest than 0, which would claim the first day had "no change".

The percentage change needs care with division:

sql
SELECT day, amount,
       ROUND(100.0 * (amount - LAG(amount) OVER (ORDER BY day))
             / NULLIF(LAG(amount) OVER (ORDER BY day), 0), 1) AS pct_change
FROM daily_sales
ORDER BY day;
dayamountpct_change
2026-03-01120NULL
2026-03-0280-33.3
2026-03-0315087.5
2026-03-0590-40.0
2026-03-0660-33.3
2026-03-09200233.3

1.0 * avoids integer division, and NULLIF would protect against a previous value of 0.

LEAD looks forward. It answers "how many days until the next sale?":

sql
SELECT day,
       LEAD(day) OVER (ORDER BY day) AS next_sale_day,
       CAST(julianday(LEAD(day) OVER (ORDER BY day)) - julianday(day) AS INTEGER) AS days_until_next
FROM daily_sales
ORDER BY day;
daynext_sale_daydays_until_next
2026-03-012026-03-021
2026-03-022026-03-031
2026-03-032026-03-052
2026-03-052026-03-061
2026-03-062026-03-093
2026-03-09NULLNULL

As with moving averages, "previous row" is not the same as "previous calendar day" when days are missing. 5 March is compared with 3 March here, which may or may not be what the report means.

sql
CREATE TABLE daily_sales (
  day    TEXT PRIMARY KEY,
  amount INTEGER NOT NULL
);
INSERT INTO daily_sales VALUES
  ('2026-03-01',120),('2026-03-02',80),('2026-03-03',150),
  ('2026-03-05',90),('2026-03-06',60),('2026-03-09',200);

SELECT day, amount,
       LAG(amount) OVER (ORDER BY day)          AS prev_amount,
       amount - LAG(amount) OVER (ORDER BY day) AS change
FROM daily_sales
ORDER BY day;

SELECT day, amount,
       ROUND(100.0 * (amount - LAG(amount) OVER (ORDER BY day))
             / NULLIF(LAG(amount) OVER (ORDER BY day), 0), 1) AS pct_change
FROM daily_sales
ORDER BY day;

SELECT day,
       LEAD(day) OVER (ORDER BY day) AS next_sale_day,
       CAST(julianday(LEAD(day) OVER (ORDER BY day)) - julianday(day) AS INTEGER) AS days_until_next
FROM daily_sales
ORDER BY day;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • LAG reads the previous row; LEAD reads the next
  • Both accept an offset and a default value
  • The first row's LAG is NULL unless a default is given
  • Previous row is not previous calendar day when days are missing

Common mistakes

  • Defaulting LAG to 0 and reporting a fake change on the first day
  • Dividing integers when computing percentage change

Follow-ups

  • How would you compare each month with the same month last year?
  • How would you find the longest gap between sales?

Tips

  • Mention the missing-day issue; it shows attention to data