Learn SQL/Advanced/Window Functions/SQL Next Row Comparison

SQL Next Row Comparison

LEAD, applied the same way — compared to what's coming next instead of what came before.

What & Why

The mirror of the previous lesson: LEAD plus a comparison, when the question is about what happens next rather than what happened last.

See How It Works

BUSINESS QUESTION

Growth wants each daily active-user value compared with the next observed day.

date_daydaunew_usersreturning_users
2024-04-0112401801060
2024-04-0213152051110
2024-04-0312881641124
2024-04-0413922211171
EXAMPLE QUERY
SELECT
  date_day,
  dau,
  LEAD(dau) OVER (ORDER BY date_day) AS next_dau,
  LEAD(dau) OVER (ORDER BY date_day) - dau AS next_day_change
FROM growth.daily_active_users
ORDER BY date_day;
RESULT — compare with next DAU
date_daydaunext_daunext_day_change
2024-04-011240131575
2024-04-0213151288-27
2024-04-0312881392104
2024-04-041392NULLNULL

LEAD supplies the immediately following day's DAU.

This lesson's practice is part of Pro.

Advanced business practice

Sign up free to try it on a real business scenario