Documentation Home
MySQL 8.4 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 40.2Mb
PDF (A4) - 40.3Mb
Man Pages (TGZ) - 262.0Kb
Man Pages (Zip) - 367.6Kb
Info (Gzip) - 4.0Mb
Info (Zip) - 4.0Mb


14.20.4 Named Windows

Windows can be defined and given names by which to refer to them inOVER clauses. To do this, use aWINDOW clause. If present in a query, theWINDOW clause falls between the positions of theHAVING andORDER BY clauses, and has this syntax:

WINDOWwindow_name AS (window_spec)    [,window_name AS (window_spec)] ...

For each window definition,window_name is the window name, andwindow_spec is the same type of window specification as given between the parentheses of anOVER clause, as described inSection 14.20.2, “Window Function Concepts and Syntax”:

window_spec:    [window_name] [partition_clause] [order_clause] [frame_clause]

AWINDOW clause is useful for queries in which multipleOVER clauses would otherwise define the same window. Instead, you can define the window once, give it a name, and refer to the name in theOVER clauses. Consider this query, which defines the same window multiple times:

SELECT  val,  ROW_NUMBER() OVER (ORDER BY val) AS 'row_number',  RANK()       OVER (ORDER BY val) AS 'rank',  DENSE_RANK() OVER (ORDER BY val) AS 'dense_rank'FROM numbers;

The query can be written more simply by usingWINDOW to define the window once and referring to the window by name in theOVER clauses:

SELECT  val,  ROW_NUMBER() OVER w AS 'row_number',  RANK()       OVER w AS 'rank',  DENSE_RANK() OVER w AS 'dense_rank'FROM numbersWINDOW w AS (ORDER BY val);

A named window also makes it easier to experiment with the window definition to see the effect on query results. You need only modify the window definition in theWINDOW clause, rather than multipleOVER clause definitions.

If anOVER clause usesOVER (window_name ...) rather thanOVERwindow_name, the named window can be modified by the addition of other clauses. For example, this query defines a window that includes partitioning, and usesORDER BY in theOVER clauses to modify the window in different ways:

SELECT  DISTINCT year, country,  FIRST_VALUE(year) OVER (w ORDER BY year ASC) AS first,  FIRST_VALUE(year) OVER (w ORDER BY year DESC) AS lastFROM salesWINDOW w AS (PARTITION BY country);

AnOVER clause can only add properties to a named window, not modify them. If the named window definition includes a partitioning, ordering, or framing property, theOVER clause that refers to the window name cannot also include the same kind of property or an error occurs:

  • This construct is permitted because the window definition and the referringOVER clause do not contain the same kind of properties:

    OVER (w ORDER BY country)... WINDOW w AS (PARTITION BY country)
  • This construct is not permitted because theOVER clause specifiesPARTITION BY for a named window that already hasPARTITION BY:

    OVER (w PARTITION BY year)... WINDOW w AS (PARTITION BY country)

The definition of a named window can itself begin with awindow_name. In such cases, forward and backward references are permitted, but not cycles:

  • This is permitted; it contains forward and backward references but no cycles:

    WINDOW w1 AS (w2), w2 AS (), w3 AS (w1)
  • This is not permitted because it contains a cycle:

    WINDOW w1 AS (w2), w2 AS (w3), w3 AS (w1)