<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Window_function_%28SQL%29</id>
	<title>Window function (SQL) - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Window_function_%28SQL%29"/>
	<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Window_function_(SQL)&amp;action=history"/>
	<updated>2026-04-22T23:46:20Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>http://debianws.lexgopc.com/wiki143/index.php?title=Window_function_(SQL)&amp;diff=6351466&amp;oldid=prev</id>
		<title>imported&gt;Djonesuk: /* History */ add SQLite</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Window_function_(SQL)&amp;diff=6351466&amp;oldid=prev"/>
		<updated>2025-02-05T00:25:38Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;History: &lt;/span&gt; add SQLite&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Short description|Function over multiple rows in SQL}}&lt;br /&gt;
{{For|the term used in signal processing|Window function}}&lt;br /&gt;
&lt;br /&gt;
In [[SQL]], a &amp;#039;&amp;#039;&amp;#039;window function&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;analytic function&amp;#039;&amp;#039;&amp;#039;&amp;lt;ref name=&amp;quot;:1&amp;quot;&amp;gt;{{Cite web|title=Analytic function concepts in Standard SQL {{!}} BigQuery|url=https://cloud.google.com/bigquery/docs/reference/standard-sql/analytic-function-concepts|access-date=2021-03-23|website=Google Cloud|language=en}}&amp;lt;/ref&amp;gt; is a function which uses values from one or multiple [[Row (database)|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.&amp;lt;ref&amp;gt;{{Cite web|title=Window Functions|url=https://sqlite.org/windowfunctions.html|access-date=2021-03-23|website=sqlite.org}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
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):&amp;lt;ref&amp;gt;{{Cite web|date=2021-02-11|title=3.5. Window Functions|url=https://www.postgresql.org/docs/13/tutorial-window.html|access-date=2021-03-23|website=PostgreSQL Documentation|language=en}}&amp;lt;/ref&amp;gt;&amp;lt;syntaxhighlight lang=&amp;quot;psql&amp;quot;&amp;gt;&lt;br /&gt;
SELECT depname, empno, salary, avg(salary) OVER (PARTITION BY depname) FROM empsalary;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
  depname  | empno | salary |          avg          &lt;br /&gt;
 ----------+-------+--------+----------------------&lt;br /&gt;
 develop   |    11 |   5200 | 5020.0000000000000000&lt;br /&gt;
 develop   |     7 |   4200 | 5020.0000000000000000&lt;br /&gt;
 develop   |     9 |   4500 | 5020.0000000000000000&lt;br /&gt;
 develop   |     8 |   6000 | 5020.0000000000000000&lt;br /&gt;
 develop   |    10 |   5200 | 5020.0000000000000000&lt;br /&gt;
 personnel |     5 |   3500 | 3700.0000000000000000&lt;br /&gt;
 personnel |     2 |   3900 | 3700.0000000000000000&lt;br /&gt;
 sales     |     3 |   4800 | 4866.6666666666666667&lt;br /&gt;
 sales     |     1 |   5000 | 4866.6666666666666667&lt;br /&gt;
 sales     |     4 |   4800 | 4866.6666666666666667&lt;br /&gt;
 (10 rows)&lt;br /&gt;
The &amp;lt;code&amp;gt;PARTITION BY&amp;lt;/code&amp;gt; clause groups rows into partitions, and the function is applied to each partition separately. If the &amp;lt;code&amp;gt;PARTITION BY&amp;lt;/code&amp;gt; clause is omitted (such as with an empty &amp;lt;code&amp;gt;OVER()&amp;lt;/code&amp;gt; clause), then the entire [[result set]] is treated as a single partition.&amp;lt;ref name=&amp;quot;:0&amp;quot;&amp;gt;{{Cite web|date=2021-02-11|title=4.2. Value Expressions|url=https://www.postgresql.org/docs/13/sql-expressions.html|access-date=2021-03-23|website=PostgreSQL Documentation|language=en}}&amp;lt;/ref&amp;gt; For this query, the average salary reported would be the average taken over all rows.&lt;br /&gt;
&lt;br /&gt;
Window functions are evaluated after aggregation (after the [[Group by (SQL)|&amp;lt;code&amp;gt;GROUP BY&amp;lt;/code&amp;gt;]] clause and non-window aggregate functions, for example).&amp;lt;ref name=&amp;quot;:1&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Syntax ==&lt;br /&gt;
According to the PostgreSQL documentation, a window function has the syntax of one of the following:&amp;lt;ref name=&amp;quot;:0&amp;quot; /&amp;gt;&amp;lt;syntaxhighlight lang=&amp;quot;psql&amp;quot;&amp;gt;&lt;br /&gt;
function_name ([expression [, expression ... ]]) OVER window_name&lt;br /&gt;
function_name ([expression [, expression ... ]]) OVER ( window_definition )&lt;br /&gt;
function_name ( * ) OVER window_name&lt;br /&gt;
function_name ( * ) OVER ( window_definition )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;where &amp;lt;code&amp;gt;window_definition&amp;lt;/code&amp;gt; has syntax:&amp;lt;syntaxhighlight lang=&amp;quot;psql&amp;quot;&amp;gt;&lt;br /&gt;
[ existing_window_name ]&lt;br /&gt;
[ PARTITION BY expression [, ...] ]&lt;br /&gt;
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]&lt;br /&gt;
[ frame_clause ]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;code&amp;gt;frame_clause&amp;lt;/code&amp;gt; has the syntax of one of the following:&amp;lt;syntaxhighlight lang=&amp;quot;psql&amp;quot;&amp;gt;&lt;br /&gt;
{ RANGE | ROWS | GROUPS } frame_start [ frame_exclusion ]&lt;br /&gt;
{ RANGE | ROWS | GROUPS } BETWEEN frame_start AND frame_end [ frame_exclusion ]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;code&amp;gt;frame_start&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;frame_end&amp;lt;/code&amp;gt; can be &amp;lt;code&amp;gt;UNBOUNDED PRECEDING&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;offset PRECEDING&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;CURRENT ROW&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;offset FOLLOWING&amp;lt;/code&amp;gt;, or &amp;lt;code&amp;gt;UNBOUNDED FOLLOWING&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;frame_exclusion&amp;lt;/code&amp;gt; can be &amp;lt;code&amp;gt;EXCLUDE CURRENT ROW&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;EXCLUDE GROUP&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;EXCLUDE TIES&amp;lt;/code&amp;gt;, or &amp;lt;code&amp;gt;EXCLUDE NO OTHERS&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;expression&amp;lt;/code&amp;gt; refers to any expression that does not contain a call to a window function.&lt;br /&gt;
&lt;br /&gt;
Notation:&lt;br /&gt;
&lt;br /&gt;
* Brackets [] indicate optional clauses&lt;br /&gt;
* Curly braces {} indicate a set of different possible options, with each option delimited by a vertical bar |&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Window functions allow access to data in the records right before and after the current record.&amp;lt;ref&amp;gt;{{Cite journal|last1=Leis|first1=Viktor|last2=Kundhikanjana|first2=Kan|last3=Kemper|first3=Alfons|last4=Neumann|first4=Thomas|date=June 2015|title=Efficient Processing of Window Functions in Analytical SQL Queries|journal=Proc. VLDB Endow.|volume=8|issue=10|pages=1058–1069|doi=10.14778/2794367.2794375|issn=2150-8097}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{Cite journal|last1=Cao|first1=Yu|last2=Chan|first2=Chee-Yong|last3=Li|first3=Jie|last4=Tan|first4=Kian-Lee|date=July 2012|title=Optimization of Analytic Window Functions|journal=Proc. VLDB Endow.|volume=5|issue=11|pages=1244–1255|arxiv=1208.0086|doi=10.14778/2350229.2350243|issn=2150-8097}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{Cite news|date=2013-11-03|title=Probably the Coolest SQL Feature: Window Functions|language=en-US|work=Java, SQL and jOOQ.|url=https://blog.jooq.org/2013/11/03/probably-the-coolest-sql-feature-window-functions/|access-date=2017-09-26}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{Cite news|date=2013-10-31|title=Window Functions in SQL - Simple Talk|language=en-US|work=Simple Talk|url=https://www.red-gate.com/simple-talk/sql/t-sql-programming/window-functions-in-sql/|access-date=2017-09-26}}&amp;lt;/ref&amp;gt; A window function defines a &amp;#039;&amp;#039;frame&amp;#039;&amp;#039; or &amp;#039;&amp;#039;window&amp;#039;&amp;#039; of rows with a given length around the current row, and performs a calculation across the set of data in the window.&amp;lt;ref&amp;gt;{{Cite web|last=|first=|date=|title=SQL Window Functions Introduction|url=https://drill.apache.org/docs/sql-window-functions-introduction/|archive-url=|archive-date=|access-date=|website=Apache Drill}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{Cite web|title=PostgreSQL: Documentation: Window Functions|url=https://www.postgresql.org/docs/current/tutorial-window.html|access-date=2020-04-04|website=www.postgresql.org|language=en}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
       NAME |&lt;br /&gt;
 ------------&lt;br /&gt;
       Aaron| &amp;lt;-- Preceding (unbounded)&lt;br /&gt;
      Andrew|&lt;br /&gt;
      Amelia|&lt;br /&gt;
       James|&lt;br /&gt;
        Jill|&lt;br /&gt;
      Johnny| &amp;lt;-- 1st preceding row&lt;br /&gt;
     Michael| &amp;lt;-- Current row&lt;br /&gt;
        Nick| &amp;lt;-- 1st following row&lt;br /&gt;
     Ophelia|&lt;br /&gt;
        Zach| &amp;lt;-- Following (unbounded)&lt;br /&gt;
In the above table, the next query extracts for each row the values of a window with one preceding and one following row:&amp;lt;syntaxhighlight lang=&amp;quot;psql&amp;quot;&amp;gt;&lt;br /&gt;
 SELECT&lt;br /&gt;
  LAG(name, 1) &lt;br /&gt;
    OVER(ORDER BY name) &amp;quot;prev&amp;quot;,&lt;br /&gt;
  name, &lt;br /&gt;
  LEAD(name, 1) &lt;br /&gt;
    OVER(ORDER BY name) &amp;quot;next&amp;quot;&lt;br /&gt;
 FROM people&lt;br /&gt;
 ORDER BY name&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;The result query contains the following values:&lt;br /&gt;
 |     PREV |     NAME |     NEXT |&lt;br /&gt;
 |----------|----------|----------|&lt;br /&gt;
 |    (null)|     Aaron|    Andrew|&lt;br /&gt;
 |     Aaron|    Andrew|    Amelia|&lt;br /&gt;
 |    Andrew|    Amelia|     James|&lt;br /&gt;
 |    Amelia|     James|      Jill|&lt;br /&gt;
 |     James|      Jill|    Johnny|&lt;br /&gt;
 |      Jill|    Johnny|   Michael|&lt;br /&gt;
 |    Johnny|   Michael|      Nick|&lt;br /&gt;
 |   Michael|      Nick|   Ophelia|&lt;br /&gt;
 |      Nick|   Ophelia|      Zach|&lt;br /&gt;
 |   Ophelia|      Zach|    (null)|&lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Window functions were incorporated into the [[SQL:2003]] standard and had functionality expanded in later specifications.&amp;lt;ref&amp;gt;{{Cite web|title=Window Functions Overview|url=https://mariadb.com/kb/en/window-functions-overview/|access-date=2021-03-23|website=MariaDB KnowledgeBase}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Support for particular database implementations was added as follows:&lt;br /&gt;
&lt;br /&gt;
* [[Oracle Database|Oracle]] - version 8.1.6 in 2000.&amp;lt;ref&amp;gt;{{Cite web | title=Oracle 8i Release 2 (8.1.6) New Features|url=https://docs.oracle.com/cd/A81042_01/DOC/server.816/a76962/816.htm | access-date=2025-01-23 | website=www.oracle.com }}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{Cite web | title=Analytic Functions in Oracle 8i|url=http://infolab.stanford.edu/infoseminar/archive/SpringY2000/speakers/agupta/paper.pdf | access-date=2025-01-23 | website=www.stanford.edu }}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* [[PostgreSQL]] - version 8.4 in 2009.&amp;lt;ref&amp;gt;{{Cite web | title=PostgreSQL Release 8.4 |url=https://www.postgresql.org/docs/8.4/release-8-4.html | access-date=2024-03-10 | website=www.postgresql.org|date=24 July 2014 }}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* [[MySQL]] - version 8 in 2018.&amp;lt;ref&amp;gt;{{Cite web |title=MySQL :: What&amp;#039;s New in MySQL 8.0? (Generally Available) |url=https://dev.mysql.com/blog-archive/whats-new-in-mysql-8-0-generally-available/ |access-date=2022-11-21 |website=dev.mysql.com}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{Cite web |title=MySQL :: MySQL 8.0 Reference Manual :: 12.21.2 Window Function Concepts and Syntax |url=https://dev.mysql.com/doc/refman/8.0/en/window-functions-usage.html |website=dev.mysql.com}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* [[MariaDB]] - version 10.2 in 2016.&amp;lt;ref&amp;gt;{{Cite web | title=MariaDB 10.2.0 Release Notes |url=https://mariadb.com/kb/en/mariadb-1020-release-notes/ | access-date=2024-03-10 | website=mariadb.com}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* [[SQLite]] - release 3.25.0 in 2018.&amp;lt;ref&amp;gt;{{cite web |title=SQLite Release 3.25.0 On 2018-09-15 |url=https://www.sqlite.org/releaselog/3_25_0.html |website=www.sqlite.org |access-date=5 February 2025}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[Select (SQL)#Limiting result rows|Select (SQL) § Limiting result rows]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
{{Reflist}}{{SQL}}&lt;br /&gt;
[[Category:Articles with example SQL code]]&lt;br /&gt;
[[Category:SQL]]&lt;/div&gt;</summary>
		<author><name>imported&gt;Djonesuk</name></author>
	</entry>
</feed>