Bird
0
0

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

medium📝 query result Q4 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_sum FROM sales;
AOnly the total sales amount for the last month per region.
BA running total of sales amount per region ordered by month, showing all rows.
CThe total sales amount for all regions combined.
DAn error because PARTITION BY cannot be combined with ORDER BY.
Step-by-Step Solution
Solution:
  1. Step 1: Understand PARTITION BY and ORDER BY in window functions

    PARTITION BY groups rows by region; ORDER BY sorts rows by month within each region.
  2. Step 2: Analyze SUM() OVER()

    SUM() OVER() calculates a running total per region ordered by month, returning a value for each row.
  3. Final Answer:

    A running total of sales amount per region ordered by month, showing all rows. -> Option B
  4. Quick Check:

    Partition + order = running total per group [OK]
Quick Trick: PARTITION BY groups; ORDER BY defines running total order [OK]
Common Mistakes:
  • Thinking it returns one row per region
  • Believing PARTITION BY and ORDER BY cannot be combined
  • Assuming it sums all regions together

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes