Introduction to Window Functions

SQL · Window Functions

Theory

A window function calculates a value for each row by looking at a set of related rows, called the window. The key difference from GROUP BY is that rows are not collapsed: GROUP BY dept turns 6 employees into 3 summary rows, while AVG(salary) OVER (PARTITION BY dept) keeps all 6 rows and adds the department average as an extra column on each one.

Every window function call has the shape function() OVER (...). Inside OVER you can say three things: PARTITION BY (which rows belong together), ORDER BY (in what sequence to walk through them) and an optional frame (which slice of the partition counts for this row, such as 'the previous 2 rows'). Window functions run after WHERE, GROUP BY and HAVING, just before the final ORDER BY, which is why you cannot use them in WHERE. To filter on one, wrap the query in a subquery or CTE.

Why we use it

Many questions need both the detail row and a group-level number side by side: each employee's salary next to the department average, each day's sales next to the running total, each row's rank. Without windows you need self-joins or subqueries that are slow and hard to read.

When to use it

Use a window function when you want to keep every row but add a calculation over related rows (ranks, running totals, previous/next values, group totals). Use plain GROUP BY when you only want one summary row per group.

How it works

  1. FROM, WHERE, GROUP BY, HAVING produce the rows
  2. PARTITION BY splits rows into independent groups
  3. ORDER BY sorts rows inside each partition
  4. Frame picks which rows count for the current row
  5. Function is computed for each row from its window
  6. Row is kept, with the result as a new column

Syntax

function() OVER (
  PARTITION BY column
  ORDER BY column
)

Example

SELECT name, department, salary,
  AVG(salary) OVER (PARTITION BY department) AS dept_avg
FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic