Bird
0
0

What is wrong with this query?

medium📝 Debug Q7 of 15
PostgreSQL - Window Functions in PostgreSQL
What is wrong with this query?
SELECT id, salary, SUM(salary) OVER (PARTITION BY department ORDER BY salary) AS running_total FROM employees;
AQuery is correct and will return running totals per department
BORDER BY cannot be used inside SUM() OVER with PARTITION BY
CSUM() cannot be used as a window function
DMissing window frame clause causes running_total to be cumulative sum of all rows in partition
Step-by-Step Solution
Solution:
  1. Step 1: Understand default window frame behavior

    Without explicit frame, SUM() OVER with ORDER BY uses RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, but RANGE with ORDER BY on salary may behave unexpectedly.
  2. Step 2: Effect of missing frame clause

    It may sum all rows with same salary value, not strictly cumulative by row; explicit ROWS frame is safer for running totals.
  3. Final Answer:

    Missing window frame clause causes running_total to be cumulative sum of all rows in partition -> Option D
  4. Quick Check:

    Specify ROWS frame for precise running totals [OK]
Quick Trick: Use ROWS frame for exact running totals, not default RANGE [OK]
Common Mistakes:
  • Assuming default frame always works for running totals
  • Thinking ORDER BY is invalid with SUM() OVER
  • Believing SUM() can't be window function

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes