Learn SQL/Advanced/Window Functions/SQL UNBOUNDED PRECEDING

SQL UNBOUNDED PRECEDING

The frame boundary that means 'all the way to the start of the partition.'

What & Why

UNBOUNDED PRECEDING as a frame's starting boundary means "don't stop early — include every row from the very first row of the partition onward." It's what makes a running total actually accumulate everything, instead of just a fixed number of nearby rows.

See How It Works

BUSINESS QUESTION

Growth wants cumulative new users from the beginning of the series.

date_daydaunew_usersreturning_users
2024-04-0112401801060
2024-04-0213152051110
2024-04-0312881641124
2024-04-0413922211171
EXAMPLE QUERY
SELECT
  date_day,
  new_users,
  SUM(new_users) OVER (ORDER BY date_day ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cumulative_new_users
FROM growth.daily_active_users
ORDER BY date_day;
RESULT — cumulative new users
date_daynew_userscumulative_new_users
2024-04-01180180
2024-04-02205385
2024-04-03164549
2024-04-04221770

UNBOUNDED PRECEDING starts every frame at the first day.

This lesson's practice is part of Pro.

Advanced business practice

Sign up free to try it on a real business scenario