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;| day | amount | prev_amount | change |
|---|---|---|---|
| 2026-03-01 | 120 | NULL | NULL |
| 2026-03-02 | 80 | 120 | -40 |
| 2026-03-03 | 150 | 80 | 70 |
| 2026-03-05 | 90 | 150 | -60 |
| 2026-03-06 | 60 | 90 | -30 |
| 2026-03-09 | 200 | 60 | 140 |
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;| day | amount | pct_change |
|---|---|---|
| 2026-03-01 | 120 | NULL |
| 2026-03-02 | 80 | -33.3 |
| 2026-03-03 | 150 | 87.5 |
| 2026-03-05 | 90 | -40.0 |
| 2026-03-06 | 60 | -33.3 |
| 2026-03-09 | 200 | 233.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;| day | next_sale_day | days_until_next |
|---|---|---|
| 2026-03-01 | 2026-03-02 | 1 |
| 2026-03-02 | 2026-03-03 | 1 |
| 2026-03-03 | 2026-03-05 | 2 |
| 2026-03-05 | 2026-03-06 | 1 |
| 2026-03-06 | 2026-03-09 | 3 |
| 2026-03-09 | NULL | NULL |
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