Problems / First & Last Value / Editorial
The key challenge is LAST_VALUE. By default, a window with ORDER BY uses a frame ending at the current row, so LAST_VALUE would just return the current row's value.
LAST_VALUE
ORDER BY
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
This is a classic gotcha — most SQL learners expect LAST_VALUE to automatically look at the whole partition, but it respects the window frame.
Solve First & Last Value yourself →