iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW tells a window calculation to start at the first row in the partition and include rows through the current row. With SUM(weight), that produces a running total. In LeetCode 1204, sorting by boarding turn and keeping totals at or below 1,000 kilograms identifies John Cena as the last person who fits.
What is a window frame in SQL?
A window frame is the subset of rows a frame-sensitive window function uses for the current row. As the query moves through the ordered rows, the frame can change. For a running total, each row’s frame includes the rows that came before it and the current row, so the sum grows as the calculation advances.
A window frame operates within a partition. If a window specification has no PARTITION BY, all rows belong to one partition. In the LeetCode example, the query needs a single running total across the queue, so it does not partition the rows.
Free tools Windows power users keep installed
One-click scans. No signup required.
What do UNBOUNDED PRECEDING and CURRENT ROW mean?
Read the frame boundaries from left to right: UNBOUNDED PRECEDING sets the start, and CURRENT ROW sets the end.
#1 Best Overall
UNBOUNDED PRECEDINGmeans there is no earlier limit within the partition. The frame starts at its first row. With ascendingORDER BY turn, that is the earliest turn.CURRENT ROWends the frame at the current row. In aROWSframe, that means the current physical row in the ordered result.
Put together, ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW includes every ordered row from the partition’s first row through the current row.
How does this frame calculate a running total?
SUM(weight) OVER (
ORDER BY turn
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS cumulative_weight
Each part has a distinct job:
ORDER BY turndetermines the sequence in which the running calculation advances. In LeetCode 1204,turnis the boarding order.UNBOUNDED PRECEDINGstarts the frame at the first row in the partition.CURRENT ROWends the frame at the row being evaluated.SUM(weight)adds the weights inside that frame.
For example, the first row’s sum is its own weight. The second row’s sum includes the first and second weights. The third row’s sum includes all three. This is a prefix sum: every result includes the ordered rows up to that point.
Why specify ROWS instead of RANGE?
ROWS and RANGE can give different results when rows tie on the window’s ORDER BY values. Such tied rows are peers.
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Repair Windows errors before they cause bigger problems3Scan for outdated or missing drivers - takes under a minute| Frame type | Meaning of CURRENT ROW as the end boundary | Practical effect |
|---|---|---|
ROWS |
The current physical row in the ordered sequence | Advances one row at a time, making the row-by-row intent explicit. |
RANGE |
The current row and its peers with the same ordering value | Tied rows can share a frame end and therefore the same aggregate result. |
LeetCode 1204 uses turns to define boarding order. For other data, if the sort key can repeat and you need a deterministic row-by-row total, add a unique tie-breaker to ORDER BY and specify ROWS. If the intended calculation should treat tied values as a group, RANGE may be appropriate.
How do you solve LeetCode 1204 with a window function?
The problem’s Queue table contains person_id, person_name, weight, and turn. The bus limit is 1,000 kilograms, and turn fixes the boarding sequence. The goal is to return the name of the last person whose cumulative weight does not exceed that limit.
Order the example rows by turn and apply the running sum:
Rank #4
| Turn | Person | Weight (kg) | Cumulative weight (kg) | Outcome |
|---|---|---|---|---|
| 1 | Alice | 250 | 250 | Fits |
| 2 | Alex | 350 | 600 | Fits |
| 3 | John Cena | 400 | 1,000 | Fits; last person within the limit |
| 4 | Marie | 200 | 1,200 | Exceeds the limit |
| 5 | Bob | 175 | Not needed to identify the last fit | After the queue has exceeded capacity |
| 6 | Winston | 500 | Not needed to identify the last fit | After the queue has exceeded capacity |
The cumulative weight reaches the limit at John Cena’s turn. Marie’s turn takes it over the limit, so no later turn can be the last person who fits. The result is John Cena.
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →This query computes the cumulative weights, filters out rows over capacity, then selects the greatest eligible turn:
Best Value
WITH running_weights AS (
SELECT
person_name,
turn,
SUM(weight) OVER (
ORDER BY turn
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS cumulative_weight
FROM Queue
)
SELECT person_name
FROM running_weights
WHERE cumulative_weight <= 1000
ORDER BY turn DESC
LIMIT 1;
The common table expression gives each queue row its prefix sum. The outer query keeps only rows whose cumulative weight is within the limit, then sorts those rows by turn descending so the latest eligible passenger is returned. The problem guarantees that the first person is within the limit, so this query has an eligible row to return.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.What happens if you omit the frame clause?
Do not assume that every SQL engine uses the same default. The cited product documentation describes peer-aware RANGE behavior when an ordered frame is omitted:
- MySQL 8.4’s frame documentation describes the default with
ORDER BYas including rows from the partition start through the current row and its peers. - PostgreSQL’s window-function documentation says an aggregate with
ORDER BYand the default frame produces running-sum-type behavior, which may not be the intended result. - PostgreSQL’s SELECT documentation describes the default frame as
RANGE UNBOUNDED PRECEDING, equivalent toRANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, through the current row’s last peer. - Microsoft’s Transact-SQL OVER documentation specifies
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROWwhen an eligible function has anORDER BYbut no explicitROWSorRANGEframe.
For an explicitly row-by-row running total, write the ROWS frame rather than relying on a default. Consult the documentation for the exact database engine and version you use.
Recommended Free Tools
Where can you verify the problem’s rules?
LeetCode 1204, “Last Person to Fit in the Bus,” defines the queue schema, boarding order, 1,000-kilogram limit, and example. The window-frame behavior is documented in the linked MySQL, PostgreSQL, and Microsoft references above.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

