Bird
0
0

Given the table sales with columns region, month, and amount, what will this query return?

medium📝 query result Q13 of 15
PostgreSQL - Window Functions in PostgreSQL
Given the table sales with columns region, month, and amount, what will this query return?
SELECT region, month, amount, SUM(amount) OVER (PARTITION BY region ORDER BY month) AS running_total FROM sales ORDER BY region, month;
AMonthly sales amount without any cumulative calculation.
BTotal sales amount for all regions combined.
CAn error because PARTITION BY cannot be combined with ORDER BY.
DA running total of sales amount per region ordered by month.
Step-by-Step Solution
Solution:
  1. Step 1: Understand SUM() OVER with PARTITION BY and ORDER BY

    SUM() OVER (PARTITION BY region ORDER BY month) calculates a running total per region ordered by month.
  2. Step 2: Analyze the output

    Each row shows the cumulative sum of amount for that region up to the current month.
  3. Final Answer:

    A running total of sales amount per region ordered by month. -> Option D
  4. Quick Check:

    PARTITION BY groups, ORDER BY defines running total order [OK]
Quick Trick: Running totals use PARTITION BY with ORDER BY inside OVER() [OK]
Common Mistakes:
  • Thinking it sums all regions together
  • Ignoring ORDER BY for running total
  • Assuming syntax error with ORDER BY in window

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes