N040-H1 Tier 3 · Intermediate · hard ecommerce · Brightlane

Return the ID, name, and price of every product, plus the running sum of `price` ordered by `price` ascending. Sort the final result by `price` ascending

Part of Aggregate Window Functions (SUM, AVG, COUNT OVER) in SQL

The problem

Brightlane's data engineer is investigating how PostgreSQL's default ordered-window frame treats products that share the same price.

Write a query to return the ID, name, and price of every product, plus the running sum of price ordered by price ascending. Sort the final result by price ascending.

Assumptions:

  • Products accumulate in ascending price order. The running sum at each row uses PostgreSQL's default ordered-window frame: every product whose price is strictly less than the current row's price, plus every product tied at the current row's price.
  • When several products share the same price, all of them receive the same running sum — that sum already includes every tied product, not just the rows up to that physical position.
  • The final result is sorted by price ascending.

Output:

  • One row per product, with columns id, name, price, and running_price_sum. Sorted by price ascending.
Schema · ecommerce 5 tables
categories
id integer
name text
parent_id? integer
products
id integer
name text
category_id integer
price numeric
stock_qty integer
attributes? jsonb
order_items
id integer
order_id integer
product_id integer
quantity integer
unit_price numeric
customers
id integer
name text
email text
city? text
country text
created_at timestamptz
is_active boolean
orders
id integer
customer_id integer
ordered_at timestamptz
status text
total_amount numeric

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution Try it yourself first
Solution query
SELECT
  id,
  name,
  price,
  SUM(price) OVER (
    ORDER BY
      price
  ) AS running_price_sum
FROM
  products
ORDER BY
  price

The shape

The default ordered-window frame is the load-bearing detail. When ORDER BY price appears inside OVER without an explicit ROWS or RANGE clause, PostgreSQL applies its default frame: every row up to and including the current row's peer group. Two products tied at the same price are peers, and they are included in each other's running sum. So both rows tied at 12.99 carry the same running_price_sum of 25.98 — that sum already contains both of them.

Clause by clause

  • SELECT id, name, price returns each product's identifier, name, and price. The running price sum is attached.
  • SUM(price) OVER (ORDER BY price) AS running_price_sum is the window expression. ORDER BY price inside OVER defines the running accumulation, but with one twist: when no explicit frame is given, PostgreSQL groups rows by equal values on the ordering expression and includes the entire peer group in each row's frame. The first two products both priced at 12.99 form a peer group of size two. Each of them sees a frame containing both rows, and each of them carries the same running sum of 12.99 + 12.99 = 25.98.
  • FROM products reads the catalog. The ORDER BY price outside the OVER clause is a separate ordering — it controls only the final display order of the result, not the window's internal accumulation.

Why the tied rows share a sum instead of receiving incremental values

The default window frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. Under RANGE mode, "current row" means every row whose ordering value equals the current row's value, not the single physical row. The two products at 12.99 are both "current row" for each other's frame. Each frame includes both of them, and the sum is computed once over that two-row frame. The result is the same on both tied rows by design.

The trap

The natural reading of SUM(price) OVER (ORDER BY price) is a strict row-by-row accumulation. The default frame quietly does something different the moment two rows share the same value on the ordering expression. The query runs, the numbers look plausible, and the tied rows carry a sum that already includes their tied neighbors. When that behavior is not what the report needs, the fix is to add an explicit ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW frame, which counts by physical row position instead of by value equality.

You practiced the default RANGE frame's peer-group inclusion — SUM(...) OVER (ORDER BY x) with no explicit frame includes every row tied with the current row, so tied rows receive identical running totals.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.

Practice, feedback, mastery, review. That's the loop that turns reading into real skill.

Start free

No account, no credit card. Start solving in under a minute.