Row Access Inside a Frame
Expand your knowledge of window functions with insights into row access inside the frame of a partition.
We'll cover the following
The last of the four components of the window specification is the optional frame specification. While there are a handful of window functions to access the rows inside a partition, we also want to consider three window functions that grant access to the rows inside the frame. These window functions can benefit from a frame specification, which is still unnecessary. The default frame spans the whole partition if that partition is not ordered. Otherwise, all preceding rows, the current row, and its peers are considered part of the frame for an ordered partition. Still, employing a frame specification, in that case, the frame can span the whole partition with BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
.
The first row
To access the first row of the frame, MySQL provides FIRST_VALUE(expression)
. The window function returns the value of expression
at the first row of the frame. As the definition of a first row implies an ordering, using this window function only makes sense with an ordered partition. In a previous example, we turned to user sessions and queried them to retrieve the first session of each user:
Get hands-on with 1400+ tech skills courses.