runtime
42 / 88

lesson 42 Window functions · advanced

FIRST_VALUE, LAST_VALUE & frames

Control which rows a window includes when choosing first and last values.

the idea

FIRST_VALUE and LAST_VALUE read endpoints of a window frame. LAST_VALUE with the default ordered frame often returns the current row. Use UNBOUNDED FOLLOWING to reach the final row in the partition.

tables in this lesson

The challenges use this data. Each column shows its type underneath; → marks a link to another table.

watch_events5 columns · 26 rows
idinteger · keyviewer_idintegervideo_idintegerwatched_attextwatch_secondsinteger
1112025-01-01 10:00:00300
2122025-01-01 11:00:00450
3212025-01-02 12:00:00600
4332025-01-02 13:00:00120
5142025-01-02 14:00:00700

showing 5 of 26 rows

example

The idea in one query. Press Run to see what it returns; running examples never affects your progress.

example.sql
SELECT
  id,
  viewer_id,
  LAST_VALUE(video_id) OVER( PARTITION BY viewer_id ORDER BY watched_at,id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS last_video
FROM watch_events;

Watch for: The default frame is not always the entire partition.

build it step by step

Each step changes one thing. Run them in order and watch the result change.

  1. Follow one viewer's video sequence. LAST_VALUE needs an explicit frame to see the end of the partition as rows accumulate.

    step-1.sql
    SELECT id, viewer_id, video_id, watched_at
    FROM watch_events
    WHERE viewer_id = 1
    ORDER BY watched_at, id;
  2. Carry the last video with a frame. ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING lets LAST_VALUE see the whole partition for each row.

    step-2.sql
    SELECT
      id,
      viewer_id,
      LAST_VALUE(video_id) OVER( PARTITION BY viewer_id ORDER BY watched_at, id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS last_video
    FROM watch_events;

challenge 1

Return id, viewer_id and last_video for each viewer's final watched video, ordered within viewer by watched_at then id. Repeat that final video on every viewer row.

Return idviewer_idlast_videoany row order

challenge-1.sql
 
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.