OVER() Clause

SQL · Window Functions

Theory

OVER() is the keyword that turns a function into a window function. Write SUM(salary) and you get an aggregate that collapses rows; write SUM(salary) OVER () and you get the same total repeated on every row, with all rows kept. Ranking and offset functions such as ROW_NUMBER(), RANK() and LAG() only exist in window form, so they always need OVER.

An empty OVER () means the window is the whole result set (after WHERE). That is perfect for 'share of total' questions: salary * 100.0 / SUM(salary) OVER () gives each person's percentage of the company salary bill in a single query. Inside the brackets you can add PARTITION BY, ORDER BY and a frame to narrow the window. Some databases also let you name a window once with a WINDOW clause and reuse it, which keeps long queries tidy.

Why we use it

OVER lets you mix row-level detail and a whole-set calculation in one SELECT. Without it, computing each row's share of a total needs a subquery or a cross join to a separate total, which is more code and easier to get wrong.

When to use it

Use OVER () when every row needs the same set-wide value, such as a grand total or overall average for percentages. Add PARTITION BY or ORDER BY inside it when the value should depend on the row's group or position.

How it works

  1. Query reaches the SELECT list
  2. Has OVER (...)? Yes: Window function, rows kept. No: Normal aggregate, rows collapse.
  3. Is OVER empty? Yes: Window = all rows. No: Apply PARTITION BY / ORDER BY / frame.
  4. Compute the value for each row from its window
  5. Every row is returned with the extra column

Syntax

function(column) OVER (...)

Example

SELECT name, salary, SUM(salary) OVER () AS company_total
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