Bird
0
0

Why does the running total calculated by SUM(amount) OVER (ORDER BY date) sometimes produce unexpected results when there are duplicate dates?

hard📝 Conceptual Q10 of 15
SQL - Advanced Window Functions
Why does the running total calculated by SUM(amount) OVER (ORDER BY date) sometimes produce unexpected results when there are duplicate dates?
ABecause SUM() cannot handle duplicate values
BBecause rows with the same date have no defined order, causing inconsistent running totals
CBecause ORDER BY date sorts rows randomly
DBecause window functions ignore duplicate rows
Step-by-Step Solution
Solution:
  1. Step 1: Understand ordering in window functions

    ORDER BY date orders rows, but if dates are duplicated, order among those rows is undefined.
  2. Step 2: Effect on running total

    Undefined order causes running total to vary between executions or produce unexpected sums.
  3. Final Answer:

    Because rows with the same date have no defined order, causing inconsistent running totals -> Option B
  4. Quick Check:

    Duplicate ORDER BY values cause undefined row order [OK]
Quick Trick: Add unique column to ORDER BY to fix running total with duplicates [OK]
Common Mistakes:
  • Assuming SUM() fails on duplicates
  • Thinking ORDER BY sorts randomly always
  • Believing window functions ignore duplicates

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes