Window frames: ROWS versus RANGE
A window frame specifies which nearby rows or ordering values contribute to the current calculation.
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 |
|---|---|
| ROWS | Counts physical row positions in the chosen order. |
| RANGE | Uses ordering values and includes peers according to the frame. |
| Explicit frame | Makes the calculation boundary visible. |
A concrete example
Two events share January 2. A ROWS running total can advance one event at a time; a RANGE frame can include both date peers together.
Remember: State the frame and tie rules instead of depending on implicit defaults.
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 |
What is a frame?
A partition says which group a row belongs to. A frame says which part of that ordered group contributes to the current calculation. Imagine sliding a small reading window across a list, or expanding it from the first record to the current one.
Not every function accepts the same frame syntax. Ranking, offset and value functions have different rules. Learn the meaning, then use the syntax supported by the chosen function.
Read the boundaries slowly
UNBOUNDED PRECEDING means the start of the partition. CURRENT ROW is the current row for a ROWS frame. UNBOUNDED FOLLOWING means the end of the partition. One PRECEDING means one earlier position in a ROWS frame.
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW therefore means “include every row from the beginning through this row.” We order by date and unique event ID so those positions are clear.
ROWS counts positions
For A, the sequence is event 1 with 100, then 2 with 50, then 3 with 50, then 4 with NULL. The running ROWS sums are 100, 150, 200 and 200. Each additional position enters separately.
Even though events 2 and 3 have the same date, they are two different positions. The ID tie-breaker defines their sequence.
RANGE uses ordering values and peers
For a cumulative RANGE ordered only by date, CURRENT ROW includes peers with the same date. Both January 2 records belong to that boundary. Their cumulative sums are therefore both 200, not 150 followed by 200.
A RANGE value-offset frame can express a date or numeric interval. It is not a row count. Supported functions, types and ordering restrictions apply, so do not attach arbitrary RANGE offsets to every function.
Write the frame instead of relying on memory
With an ordered SUM and no explicit frame, Snowflake’s implicit frame is cumulative RANGE. FIRST_VALUE, LAST_VALUE and NTH_VALUE use a different default: the whole partition. A remembered default from another function or database can produce the wrong answer.
Our examples spell out the frame whenever frame semantics matter. This makes the question visible and keeps unexpected peers from changing the intended result silently.
Words used in this lesson
- Frame
- The subset of a partition contributing to a frame-sensitive calculation.
- ROWS
- A frame described by ordered record positions.
- Peers
- Records sharing the relevant window ordering values.
See how it works
Choose a step to explore the idea at your own pace.
Open the interactive explanation
Window frames: ROWS versus RANGE
ROWS at event 2
The ordered positions include 1 and 2 so far. Event 3 has not entered yet.
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
- ROWS at event 2
The ordered positions include 1 and 2 so far. Event 3 has not entered yet.
- RANGE at Jan 2
The date boundary includes both January 2 peers, not just whichever one appears first on the screen.
- ROWS at event 3
The third position enters and the running sum catches up to 200.
- Unknown amount
The final unknown amount does not add a known numeric value. The known total remains 200.
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.
Compare cumulative ROWS and RANGE
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, event_date, amount,
SUM(amount) OVER (ORDER BY event_date, event_id
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS row_total,
SUM(amount) OVER (ORDER BY event_date
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS date_total
FROM sales
WHERE customer = 'A'
ORDER BY event_date, event_id;What to expect and why
ROWS has a unique positional order. RANGE intentionally orders only by date to group same-date peers. Event 2 demonstrates the difference: 150 versus 200.
| EVENT_ID | EVENT_DATE | AMOUNT | ROW_TOTAL | DATE_TOTAL |
|---|---|---|---|---|
| 1 | 2025-01-01 | 100 | 100 | 100 |
| 2 | 2025-01-02 | 50 | 150 | 200 |
| 3 | 2025-01-02 | 50 | 200 | 200 |
| 4 | 2025-01-05 | NULL | 200 | 200 |
Check your understanding
Choose an answer, then check your reasoning. This is practice, not a scored test.
Try it yourself
Explain why ROWS 2 PRECEDING is not the same as the preceding two calendar days.
Compare your reasoning
ROWS counts records in the ordered sequence. Several records may share a day, or there may be gaps of many days between records.
Official sources and curriculum
Module 04 · See module scope