Window functions

By using window functions, for each row from a query, you can perform a calculation using rows related to that row. This is accomplished by using the OVER and WINDOW clauses.

Here is the list of calculations that you can do:

  • CUME_DIST(): Cumulative distribution value
  • DENSE_RANK(): Rank of the current row within its partition, without gaps
  • FIRST_VALUE(): The value of the argument from the first row of the window frame
  • LAG(): The argument value from the row lagging the current row within partition
  • LAST_VALUE(): Value of argument from the first row of window frame
  • LEAD(): Value of argument from row leading current row within partition
  • NTH_VALUE(): Argument value from n-th row of window frame
  • NTILE(): Bucket number of the current ...

Get MySQL 8 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.