Window Function Descriptions | 08:00:00 | xh458 | 25 | 0 | 25 | 10 | 30 | +----------+---------+------+-------+------+--------+--------+ Each function uses the rows in the current frame, which, per the window definition shown, extends from the first partition row to the current row. For the  NTH_VALUE()  calls, the current frame does not always include the requested row; in such cases, the return value is  NULL . Returns the value of  expr  from the row that lags (precedes) the current row by  N  rows within its partition. If there is no such row, the return value is  default . For example, if  N  is 3, the return value is  default for the first two rows. If  N  or  default  are missing, the defaults are 1 and  NULL , respectively. N  must be a literal nonnegative integer. If  N  is 0,  expr  is evaluated for the current row. Beginning with MySQL 8.0.22,  N  cannot be  NULL . In addition, it must now be an integer in the range  1  to 2 $^{63}$, inclusive, in any of the following forms: over_clause  is as described in Section 12.21.2, “Window Function Concepts and Syntax” . null_treatment  is as described in the section introduction. LAG()  (and the similar  LEAD()  function) are often used to compute differences between rows. The following query shows a set of time-ordered observations and, for each one, the  LAG()  and  LEAD() values from the adjoining rows, as well as the differences between the current and adjoining rows: mysql>  SELECT t, val, LAG(val) OVER w AS ’lag’, LEAD(val) OVER w AS ’lead’, val - LAG(val) OVER w AS ’lag diff’, val - LEAD(val) OVER w AS ’lead diff’ FROM series WINDOW w AS (ORDER BY t); +----------+------+------+------+----------+-----------+ | t | val | lag | lead | lag diff | lead diff | +----------+------+------+------+----------+-----------+ | 12:00:00 | 100 | NULL | 125 | NULL | -25 | | 13:00:00 | 125 | 100 | 132 | 25 | -7 | | 14:00:00 | 132 | 125 | 145 | 7 | -13 | | 15:00:00 | 145 | 132 | 140 | 13 | 5 | | 16:00:00 | 140 | 145 | 150 | -5 | -10 | | 17:00:00 | 150 | 140 | 200 | 10 | -50 | | 18:00:00 | 200 | 150 | NULL | 50 | NULL | 2420 •a local variable in a stored routine •a user-defined variable •a positional parameter marker ( ? ) •an unsigned integer constant literal • LAG( expr  [,  N [,  default ]])  [ null_treatment ]  over_clause