---
title: "Window Functions and Running Calculations"
description: "A window function computes over a set of neighbouring rows while keeping every row in the output, which grouping cannot do. Ranking, running totals and comparisons with the previous row all come from "
canonical: https://lightmysky.com/learn/computing/window-functions-and-running-calculations-mt_aaVxFPjWQv
source: https://lightmysky.com/learn/computing/window-functions-and-running-calculations-mt_aaVxFPjWQv.md
retrieved: 2026-09-12
---

> **Agent view.** This is the Markdown twin of the page, for tools and assistants.
> When to use this site, and the call that answers each job: https://lightmysky.com/agent-instructions.md
> API description (OpenAPI 3.1): https://lightmysky.com/openapi.json · Authentication: https://lightmysky.com/auth.md
> Pricing: https://lightmysky.com/pricing.md · Catalog: https://lightmysky.com/llms.txt · Full catalog: https://lightmysky.com/llms-full.txt
> Every machine-readable file on this domain: https://lightmysky.com/.well-known/ai-catalog.json
> Ask for Markdown with `Accept: text/markdown`, a `.md` address, or `?mode=agent`.

# Window Functions and Running Calculations

A window function computes over a set of neighbouring rows while keeping every row in the output, which grouping cannot do. Ranking, running totals and comparisons with the previous row all come from partitioning and ordering the window.

Subject: Computing · Area: Data & Databases · Ages 19 to 20
Page: https://lightmysky.com/learn/computing/window-functions-and-running-calculations-mt_aaVxFPjWQv

## Ready when they can

- Compute a running total per customer with a window rather than a self join
- Rank rows within a partition and handle ties deliberately
- Say what a window function keeps that a group by would have collapsed

## Lesson: Totals that keep every row

A window function computes across neighbor rows while keeping every row in the output. Grouping instead collapses each group to one row. When you need each order next to its running total, only a window keeps both.

**Example.** Per customer running totals read: SUM(amount) OVER (PARTITION BY customer ORDER BY date). Partition restarts the sum per customer, ordering walks the dates, and each order row stays visible beside its total.

Ranking inside a partition needs a tie plan. RANK leaves gaps after ties, DENSE_RANK packs tight with no gaps, and ROW_NUMBER breaks ties by order without sharing a rank. State the tie rule before you rank.

**Tip.** Ask first what must survive: rows or groups. If detail rows must survive with a calculation beside them, reach for a window. If one row per group is the goal, group instead.

**Recap.** Windows calculate beside surviving rows, partitions restart them, and ties need a stated rule.

## Practice

8 questions on this page, each with its working shown.

## Needs first

- [SQL: Joining Tables and Summarising Groups](https://lightmysky.com/learn/computing/sql-joining-tables-and-summarising-groups-mt_0QPZDE2KuS)
- [SQL at Depth: Subqueries, Set Operations and Views](https://lightmysky.com/learn/computing/sql-at-depth-subqueries-set-operations-and-views-mt_SLKUlC-7or)

## Opens up

- [Indexes: B-Trees and the Cost of a Lookup](https://lightmysky.com/learn/computing/indexes-b-trees-and-the-cost-of-a-lookup-mt_1Z5EQTqooI)
