Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk3 min

SQL Window Frames: When to Use ROWS for a Running Total

See exactly which rows UNBOUNDED PRECEDING and CURRENT ROW include, why ROWS differs from RANGE on ties, and how the frame solves LeetCode 1204.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW tells a window aggregate to include every ordered row from the start of the current partition through the current row. For LeetCode 1204, that makes SUM(weight) a running total in boarding order; the last passenger whose total stays at or below 1,000 kilograms is John Cena.

What is a window frame in SQL?

A window function calculates a value using related rows while keeping each input row in the result. A window frame is the subset of those rows used for the current row’s calculation. As the current row changes, the frame can move with it. PostgreSQL explains how frame-sensitive aggregate functions operate on the rows in the current frame in its Window Functions documentation.

As an Amazon Associate I earn from qualifying purchases.

For a running total, each row needs the values from the beginning of the ordered data through that row. The frame clause states those boundaries explicitly.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What do UNBOUNDED PRECEDING and CURRENT ROW mean?

Consider this window expression:

SUM(weight) OVER (
  ORDER BY turn
  ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS cumulative_weight
  • ORDER BY turn sets the sequence in which the calculation advances. In LeetCode 1204, the turn column gives the boarding order.
  • UNBOUNDED PRECEDING sets the frame’s starting boundary at the first row of the current partition. It does not mean the query scans rows before that partition.
  • CURRENT ROW sets the ending boundary. With ROWS, it means the current row itself.
  • SUM(weight) adds the weights in that frame. Each output is therefore the total weight from the first turn through the current turn.

In short, “unbounded” means there is no earlier starting offset within the partition; it does not mean an infinite set of rows.

How ROWS differs from RANGE when sort values tie

The distinction matters when multiple rows have the same value in the window’s ORDER BY. Such rows are peers. With ROWS, CURRENT ROW ends at the current physical row in the ordered sequence. With RANGE, the current-row boundary includes the current row’s peers, so tied rows can receive the same aggregate result. MySQL describes frames and peer handling in its Window Function Frame Specification; PostgreSQL also explains peer-aware frames in its Window Functions documentation.

For a row-by-row running total, write ROWS explicitly. If the ordering column is not unique, add a unique tie-breaker when the desired row sequence requires one. LeetCode 1204 defines distinct turns, so ORDER BY turn establishes the example’s order.

Rank #2
SQL Flashcards & NoSQL Flashcards | Database Concepts Study Cards for Beginners | Interview Prep for Software Engineers, Data Analysts & Students | Learn SQL Faster
  • Comprehensive Coverage: SQL Flashcards and NoSQL Flashcards designed for beginners and interview prep, covering core database concepts, queries, indexing, normalization, and real-world use cases. From relational structures, JOINs, and indexing to NoSQL document models, key-value stores, and distributed systems, these flashcards give you a solid foundation and advanced knowledge to handle any database challenge confidently.
  • Interactive Learning: Enhance your understanding with an interactive, hands-on approach. Each card includes practical query examples, schema illustrations, and exercises that let you immediately apply what you learn. This active learning style helps you strengthen your querying skills and build intuition for solving real data problems. Beginner-friendly explanations that help you learn SQL and NoSQL faster without overwhelming theory or dense textbooks
  • Portable Convenience: Study databases anytime, anywhere. Whether you’re at home, commuting, or taking a break, these portable flashcards make it easy to learn on the go. Perfect for busy students, developers, or professionals fitting learning into a tight schedule.
  • Versatile Audience: Designed for all learners from students preparing for exams to data analysts, backend engineers, and tech enthusiasts. Whether you're building your first query or optimizing production databases, these flashcards guide you at every stage of your learning journey. Perfect for SQL interview preparation for software engineers, data analysts, backend developers, and computer science students
  • Skill Enhancement: Boost your confidence and stay current with evolving database technologies. Ideal for self-study, bootcamps, university courses, and last-minute interview revision with concise, memorable flashcard format

LeetCode 1204: find the last passenger who fits

The problem gives a Queue table with person_id, person_name, weight, and turn. Passengers board by turn, and the bus limit is 1,000 kilograms. The original statement and example are on LeetCode 1204: Last Person to Fit in the Bus.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Sort the example by boarding order, then apply the running sum:

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 passenger within the limit
4 Marie 200 1,200 Exceeds the limit
5 Bob 175 Not needed The limit was already exceeded at turn 4
6 Winston 500 Not needed The limit was already exceeded at turn 4

At turn 3, the cumulative weight is exactly 1,000 kilograms, so John Cena still fits. Marie’s addition raises the total to 1,200 kilograms. Because everyone boards in turn order, later passengers cannot become the last person to fit after the queue has already crossed the limit.

Query using an explicit window frame

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 inner query gives each passenger the cumulative weight through their turn. The outer query keeps only passengers whose cumulative total is no greater than the capacity, then returns the eligible passenger with the greatest turn. The problem’s test cases ensure that the first passenger is within the limit.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Should you rely on the default frame?

Defaults depend on the database engine and the function. The cited manuals for MySQL 8.4, PostgreSQL, and SQL Server describe an ordered window without an explicit frame as using RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW for the relevant aggregate-window behavior. That includes peers at the current ordering value, rather than necessarily stopping at one physical row. See the MySQL 8.4 frame specification, PostgreSQL’s Window Functions and SELECT documentation, and Microsoft’s OVER Clause (Transact-SQL).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

An explicit ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW makes the row-by-row intent visible instead of relying on a default. For production queries, check the documentation for the specific database engine and version you use.

Best Value
Funny Programmer SQL Database Query Programmer T-Shirt
  • Funny programmer gift for software developers and computer scientists. This coding design shows a fun SQL query for database admins and nerds.
  • Cool SQL Database gift for men and women who love SQL. The perfect SQL Query gift for programmers, hackers and SQL database fans who love relational databases.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.