Window function (SQL)

Function over multiple rows in SQL


title: "Window function (SQL)" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["articles-with-example-sql-code", "sql"] description: "Function over multiple rows in SQL" topic_path: "technology/databases" source: "https://en.wikipedia.org/wiki/Window_function_(SQL)" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0

::summary Function over multiple rows in SQL ::

In SQL, a window function or analytic function is a function which uses values from one or multiple rows to return a value for each row. (This contrasts with an aggregate function, which returns a single value for multiple rows.) Window functions have an OVER clause; any function without an OVER clause is not a window function, but rather an aggregate or single-row (scalar) function.

Example

As an example, here is a query which uses a window function to compare the salary of each employee with the average salary of their department (example from the PostgreSQL documentation): SELECT depname, empno, salary, avg(salary) OVER (PARTITION BY depname) FROM empsalary; Output: depname | empno | salary | avg ----------+-------+--------+---------------------- develop | 11 | 5200 | 5020.0000000000000000 develop | 7 | 4200 | 5020.0000000000000000 develop | 9 | 4500 | 5020.0000000000000000 develop | 8 | 6000 | 5020.0000000000000000 develop | 10 | 5200 | 5020.0000000000000000 personnel | 5 | 3500 | 3700.0000000000000000 personnel | 2 | 3900 | 3700.0000000000000000 sales | 3 | 4800 | 4866.6666666666666667 sales | 1 | 5000 | 4866.6666666666666667 sales | 4 | 4800 | 4866.6666666666666667 (10 rows) The PARTITION BY clause groups rows into partitions, and the function is applied to each partition separately. If the PARTITION BY clause is omitted (such as with an empty OVER() clause), then the entire result set is treated as a single partition. For this query, the average salary reported would be the average taken over all rows.

Window functions are evaluated after aggregation (after the GROUP BY clause and non-window aggregate functions, for example).

Syntax

According to the PostgreSQL documentation, a window function has the syntax of one of the following: function_name ([expression [, expression ... ]]) OVER window_name function_name ([expression [, expression ... ]]) OVER ( window_definition ) function_name ( * ) OVER window_name function_name ( * ) OVER ( window_definition ) where window_definition has syntax: [ existing_window_name ] [ PARTITION BY expression [, ...] ] [ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ] [ frame_clause ] frame_clause has the syntax of one of the following: { RANGE | ROWS | GROUPS } frame_start [ frame_exclusion ] { RANGE | ROWS | GROUPS } BETWEEN frame_start AND frame_end [ frame_exclusion ] frame_start and frame_end can be UNBOUNDED PRECEDING, offset PRECEDING, CURRENT ROW, offset FOLLOWING, or UNBOUNDED FOLLOWING. frame_exclusion can be EXCLUDE CURRENT ROW, EXCLUDE GROUP, EXCLUDE TIES, or EXCLUDE NO OTHERS.

expression refers to any expression that does not contain a call to a window function.

Notation:

  • Brackets [] indicate optional clauses
  • Curly braces {} indicate a set of different possible options, with each option delimited by a vertical bar |

Example

Window functions allow access to data in the records right before and after the current record. A window function defines a frame or window of rows with a given length around the current row, and performs a calculation across the set of data in the window. NAME |

Aaron| Andrew| Amelia| James| Jill| Johnny| Michael| Nick| Ophelia| Zach| In the above table, the next query extracts for each row the values of a window with one preceding and one following row: ::code[lang=tsql] SELECT LAG(name, 1) OVER(ORDER BY name) "prev", name, LEAD(name, 1) OVER(ORDER BY name) "next" FROM people ORDER BY name ::

The result query contains the following values:

PREVNAMENEXT
(null)AaronAndrew
AaronAndrewAmelia
AndrewAmeliaJames
AmeliaJamesJill
JamesJillJohnny
JillJohnnyMichael
JohnnyMichaelNick
MichaelNickOphelia
NickOpheliaZach
OpheliaZach(null)

History

Window functions were incorporated into the SQL:2003 standard and had functionality expanded in later specifications.

Support for particular database implementations was added as follows:

References

References

  1. "Analytic function concepts in Standard SQL {{!}} BigQuery".
  2. "Window Functions".
  3. (2021-02-11). "3.5. Window Functions".
  4. (2021-02-11). "4.2. Value Expressions".
  5. (June 2015). "Efficient Processing of Window Functions in Analytical SQL Queries". Proc. VLDB Endow..
  6. (July 2012). "Optimization of Analytic Window Functions". Proc. VLDB Endow..
  7. (2013-11-03). "Probably the Coolest SQL Feature: Window Functions". Java, SQL and jOOQ..
  8. (2013-10-31). "Window Functions in SQL - Simple Talk". Simple Talk.
  9. "SQL Window Functions Introduction".
  10. "PostgreSQL: Documentation: Window Functions".
  11. "Window Functions Overview".
  12. "Oracle 8i Release 2 (8.1.6) New Features".
  13. "Analytic Functions in Oracle 8i".
  14. (24 July 2014). "PostgreSQL Release 8.4".
  15. "MySQL :: What's New in MySQL 8.0? (Generally Available)".
  16. "MySQL :: MySQL 8.0 Reference Manual :: 12.21.2 Window Function Concepts and Syntax".
  17. "MariaDB 10.2.0 Release Notes".
  18. "SQLite Release 3.25.0 On 2018-09-15".

::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::

articles-with-example-sql-codesql