ROWS vs RANGE Window Frames on Duplicate Dates

Quick Overview

Write a SQL query that computes two running totals of order amounts, one with a ROWS frame and one with a RANGE frame, on data where several orders share a date. It tests window frame semantics, peer rows, the default frame and deterministic ordering of ties.

ROWS vs RANGE Window Frames on Duplicate Dates

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

The `orders` table has one row per order with its date and amount, and several orders can share a date. Compute two running totals of `amount` in date order across the whole table. Both use a frame from `UNBOUNDED PRECEDING` to `CURRENT ROW`, but one counts the frame in `ROWS` and the other in `RANGE`. The two columns differ only on orders that share a date, and the output should show that difference. ```hint Which row comes first Within one date, something has to decide the order in which tied rows are added. Check whether your window ordering decides it, and what `CURRENT ROW` means for each frame unit when rows tie. ``` ### Input Table(s) Table `orders`, primary key `order_id`: | Column | Type | Meaning | |---|---|---| | `order_id` | INT | Primary key, one row per order | | `order_date` | DATE | Date of the order, NOT NULL, not unique | | `amount` | DECIMAL(10,2) | Order amount, NOT NULL, may be negative | ### Output Contract Return one row per order with these columns, in this order: | Column | Meaning | |---|---| | `order_id` | The order | | `order_date` | Date of the order | | `amount` | Order amount | | `rows_running` | `SUM(amount)` with `ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW`, window ordered by `order_date`, then `order_id`. Orders on the same date get successive partial sums. | | `range_running` | `SUM(amount)` with `RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW`, window ordered by `order_date` only. Orders on the same date are peers and all get the total through the end of that date. | - There is no partitioning: each running total covers the whole table. - Order the rows by `order_date`, then `order_id`, both ascending. `order_id` is unique, so this order is unique. ### Example Input `orders`: | order_id | order_date | amount | |---|---|---| | 1 | 2024-01-01 | 100.00 | | 2 | 2024-01-02 | 50.00 | | 3 | 2024-01-02 | 30.00 | | 4 | 2024-01-03 | 20.00 | Expected output: | order_id | order_date | amount | rows_running | range_running | |---|---|---|---|---| | 1 | 2024-01-01 | 100.00 | 100.00 | 100.00 | | 2 | 2024-01-02 | 50.00 | 150.00 | 180.00 | | 3 | 2024-01-02 | 30.00 | 180.00 | 180.00 | | 4 | 2024-01-03 | 20.00 | 200.00 | 200.00 | Orders 2 and 3 share 2024-01-02. The ROWS total adds them one at a time in `order_id` order (100 + 50 = 150, then 150 + 30 = 180). The RANGE total treats both as peers and gives each the total through the end of the date, 180. On 2024-01-03 both columns are back at 200. ### Constraints and Clarifications - The table has 1 to 10,000 rows. - No column contains NULL. - `amount` may be negative, so either running total can go down. - Any number of orders can share an `order_date`. Among them, `order_id` ascending decides the order in which `rows_running` adds them. - On the last order of each date (by `order_id`), `rows_running` and `range_running` are equal.

Overview: Write a SQL query that computes two running totals of order amounts, one with a ROWS frame and one with a RANGE frame, on data where several orders share a date. It tests window frame semantics, peer rows, the default frame and deterministic ordering of ties.

Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

The orders table has one row per order with its date and amount, and several orders can share a date. Compute two running totals of amount in date order across the whole table. Both use a frame from UNBOUNDED PRECEDING to CURRENT ROW, but one counts the frame in ROWS and the other in RANGE. The two columns differ only on orders that share a date, and the output should show that difference.

Input Table(s)

Table orders, primary key order_id:

ColumnTypeMeaning
order_idINTPrimary key, one row per order
order_dateDATEDate of the order, NOT NULL, not unique
amountDECIMAL(10,2)Order amount, NOT NULL, may be negative

Output Contract

Return one row per order with these columns, in this order:

ColumnMeaning
order_idThe order
order_dateDate of the order
amountOrder amount
rows_runningSUM(amount) with ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, window ordered by order_date, then order_id. Orders on the same date get successive partial sums.
range_runningSUM(amount) with RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, window ordered by order_date only. Orders on the same date are peers and all get the total through the end of that date.
  • There is no partitioning: each running total covers the whole table.
  • Order the rows by order_date , then order_id , both ascending. order_id is unique, so this order is unique.

Example

Input orders:

order_idorder_dateamount
12024-01-01100.00
22024-01-0250.00
32024-01-0230.00
42024-01-0320.00

Expected output:

order_idorder_dateamountrows_runningrange_running
12024-01-01100.00100.00100.00
22024-01-0250.00150.00180.00
32024-01-0230.00180.00180.00
42024-01-0320.00200.00200.00

Orders 2 and 3 share 2024-01-02. The ROWS total adds them one at a time in order_id order (100 + 50 = 150, then 150 + 30 = 180). The RANGE total treats both as peers and gives each the total through the end of the date, 180. On 2024-01-03 both columns are back at 200.

Constraints and Clarifications

  • The table has 1 to 10,000 rows.
  • No column contains NULL.
  • amount may be negative, so either running total can go down.
  • Any number of orders can share an order_date . Among them, order_id ascending decides the order in which rows_running adds them.
  • On the last order of each date (by order_id ), rows_running and range_running are equal.