SQL FIRST_VALUE

Returns the value from the very first row of the window, repeated on every row.

What & Why

FIRST_VALUE returns the value from the first row in the current ordered window frame. It compares every row with a baseline value without joining the first row back to the detail table.

See How It Works

BUSINESS QUESTION

Compare each daily active-user value with the first observed day.

date_daydaunew_usersreturning_users
2024-04-0112401801060
2024-04-0213152051110
2024-04-0312881641124
2024-04-0413922211171
EXAMPLE QUERY
SELECT
  date_day,
  dau,
  FIRST_VALUE(dau) OVER (
    ORDER BY date_day
    ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
  ) AS first_dau
FROM growth.daily_active_users
ORDER BY date_day;
RESULT — first DAU repeated as the baseline
date_daydaufirst_dau
2024-04-0112401240
2024-04-0213151240
2024-04-0312881240
2024-04-0413921240

FIRST_VALUE reads 1,240 from the first ordered row and repeats it beside every later day.

This lesson's practice is part of Pro.

Advanced business practice

Sign up free to try it on a real business scenario