SUM() OVER

SQL · Window Functions

Theory

SUM(column) OVER (...) adds values across the window and puts the result on every row, without collapsing rows. What it adds depends on what you put inside OVER. With only PARTITION BY region, each row gets its region's total. With ORDER BY month (and optionally PARTITION BY), each row gets a running total from the first month up to the current one, restarting for every region.

This is the standard way to build cumulative figures, such as year-to-date sales or a bank balance from a list of transactions. Remember the default frame with ORDER BY is RANGE, so rows with the same ORDER BY value are added together in one step; write ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW for a strict row-by-row running total. You can also combine both forms: revenue * 100.0 / SUM(revenue) OVER (PARTITION BY region) gives each month's share of its region.

Why we use it

Running totals and 'share of group' figures are some of the most requested report columns. SUM() OVER computes them in one scan, where the old way needed a correlated subquery that re-added all earlier rows for every row.

When to use it

Use SUM() OVER for running totals, year-to-date figures and per-group totals next to detail rows. Use plain SUM with GROUP BY when you only need one total per group.

How it works

  1. Rows split by PARTITION BY
  2. ORDER BY inside OVER? Yes: Frame = first row to current row. No: Frame = whole partition.
  3. Add the non-NULL values in the frame
  4. Write the total on the current row
  5. New partition starts? Yes: Total restarts from zero. No: Keep accumulating.
  6. Running or group total on every row

Syntax

SUM(column) OVER (ORDER BY col)

Example

SELECT name, salary,
  SUM(salary) OVER (ORDER BY id) AS running_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