Window functions: keep the rows, add context
A window function adds a calculation to each row while preserving the original row detail.
Understand the idea
Each row is a fictional sale event. Dates use one calendar and amounts use one currency. Tied dates, tied amounts, an unknown amount and a known zero make the edge cases visible. Every SQL block includes these rows.
| Concept | What it means |
|---|---|
| OVER | Defines the calculation context. |
| PARTITION BY | Splits rows into independent groups. |
| Window ORDER BY | Defines calculation order, not final display order. |
A concrete example
Customer A has four events; each can show the same customer total of 200 while the four event rows remain.
Remember: Do not sum a total repeated on every event row.
Explain it step by step · full theory
The seven input events
Each row is a fictional sale event. Dates use one calendar and amounts use one currency. Tied dates, tied amounts, an unknown amount and a known zero make the edge cases visible. Every SQL block includes these rows.
| EVENT_ID | CUSTOMER | EVENT_DATE | AMOUNT |
|---|---|---|---|
| 1 | A | 2025-01-01 | 100 |
| 2 | A | 2025-01-02 | 50 |
| 3 | A | 2025-01-02 | 50 |
| 4 | A | 2025-01-05 | NULL |
| 5 | B | 2025-01-01 | 80 |
| 6 | B | 2025-01-03 | 20 |
| 7 | B | 2025-01-04 | 0 |
Why do we need a window function?
Imagine that a teacher writes each student’s score next to the class average. The student rows remain visible, but each row now has context. A window function works in a similar way: it calculates across related rows while keeping the detail rows.
In our dataset, customer A has four sale events. A GROUP BY customer summary produces one row for A. SUM(amount) OVER (PARTITION BY customer) instead adds A’s total beside each of A’s four records.
Read the expression in small pieces
SUM(amount) names the calculation. OVER tells SQL to use a window rather than collapse the records into a grouped result. PARTITION BY customer divides the input into separate groups for that calculation. A window partition is a logical group of rows, not a storage micro-partition.
Customer A has known amounts 100, 50 and 50, plus one NULL. The known total is 200. Customer B has 80, 20 and 0, totaling 100. Each customer’s total is repeated on their own records.
Whole input or separate partitions?
SUM(amount) OVER () uses the entire input visible to that window calculation. It gives 300 beside every record in this example. Adding PARTITION BY customer changes the comparison group, not the number of rows.
A window sees the rows produced by earlier logical query stages. If WHERE first removes customer B, an OVER () calculation no longer includes B. “All rows” means all rows in that query stage, not all records everywhere in the source.
The two ORDER BY clauses have different jobs
ORDER BY inside OVER controls the sequence used by the calculation. ORDER BY at the end of the SELECT controls how the result is displayed. One does not replace the other.
Some functions need a sequence, such as numbering events from first to last. A total over a complete partition does not require an order. Adding an order can introduce frame behavior, so do not add it merely to make the output look sorted.
Do not total the repeated totals
A’s total of 200 appears four times because there are four A records. Adding that repeated column later gives 800, not A’s actual known total. Keep track of what one output row means.
NULL amounts are ignored by SUM, not proven to be zero. The total of known amounts is useful, but a missing-value count may still be necessary to describe completeness.
Words used in this lesson
- Window function
- A calculation across related rows that can keep each detail row.
- OVER
- The clause that defines a window calculation.
- PARTITION BY
- Separates rows into groups for a window function.
See how it works
Choose a step to explore the idea at your own pace.
Open the interactive explanation
Window functions: keep the rows, add context
Group
GROUP BY customer collapses detail into two customer summaries.
Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.
Read the complete diagram explanation
- Group
GROUP BY customer collapses detail into two customer summaries.
- Window
The window total is attached to each original event instead.
- Partition
A and B are separate calculation groups. This is not physical storage partitioning.
- Output
A final ORDER BY controls presentation. Seven records remain.
Try the SQL or command
Read → predict → run in your learning environment → compare. You can also learn by reading without an account.
New to SQL? Where to run it and how to read it
A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.
- No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
- For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
- Read the entire example, including comments beginning with
--. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor. - Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
- SELECT / FROM
- Choose the values to return / identify their input.
- VALUES / COLUMN1
- VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
- AS / WITH
- AS gives a result a readable name. WITH names a temporary query step within one statement.
- NULL / ::
- NULL means missing or unknown. The :: notation converts a value to a named data type.
- Session
- One connection and its settings. Keep related transaction steps in the same session.
- Database.schema.table
- A full address for stored data. It is separate from the warehouse that processes queries.
Need the L2_OBJECTS_LAB setup? Open the first database-object lesson →
Help with common practice errors
- No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
- Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
- Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
- Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.
Before running this lesson
Seven fictional events are included in each SQL block below. Run each block independently in an authorized learning session with a warehouse; no stored SALES table is required. Results are illustrative.
Add a customer total to each event
WITH sales AS (
SELECT column1::NUMBER AS event_id,
column2::VARCHAR AS customer,
TO_DATE(column3::VARCHAR, 'YYYY-MM-DD') AS event_date,
column4::NUMBER(12,2) AS amount
FROM VALUES
(1, 'A', '2025-01-01', 100),
(2, 'A', '2025-01-02', 50),
(3, 'A', '2025-01-02', 50),
(4, 'A', '2025-01-05', NULL),
(5, 'B', '2025-01-01', 80),
(6, 'B', '2025-01-03', 20),
(7, 'B', '2025-01-04', 0)
)
SELECT event_id, customer, amount,
SUM(amount) OVER (PARTITION BY customer) AS customer_total
FROM sales
ORDER BY customer, event_id;What to expect and why
A’s known total is 200 and B’s is 100. The NULL amount on event 4 stays NULL while the partition total is still 200.
| EVENT_ID | CUSTOMER | AMOUNT | CUSTOMER_TOTAL |
|---|---|---|---|
| 1 | A | 100 | 200 |
| 2 | A | 50 | 200 |
| 3 | A | 50 | 200 |
| 4 | A | NULL | 200 |
| 5 | B | 80 | 100 |
| 6 | B | 20 | 100 |
| 7 | B | 0 | 100 |
Check your understanding
Choose an answer, then check your reasoning. This is practice, not a scored test.
Try it yourself
Predict SUM(amount) OVER () for every record in this dataset.
Compare your reasoning
Each row receives 300, the sum of known amounts across both customers. It is repeated context, not seven separate totals to add together.
Official sources and curriculum
Module 04 · See module scope