Bird
0
0

Given the table sales with columns region, month, and revenue, 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 revenue, what will this query return?
SELECT region, month, revenue, FIRST_VALUE(revenue) OVER (PARTITION BY region ORDER BY month) AS first_rev FROM sales ORDER BY region, month;
AThe total revenue summed for each region.
BThe revenue of the last month for each region repeated on every row of that region.
CThe revenue of the first month for each region repeated on every row of that region.
DThe revenue of the current month only.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze FIRST_VALUE with PARTITION and ORDER BY

    FIRST_VALUE(revenue) OVER (PARTITION BY region ORDER BY month) returns the first revenue value per region ordered by month.
  2. Step 2: Understand output per row

    For each row in a region, the first month's revenue is repeated as first_rev.
  3. Final Answer:

    The revenue of the first month for each region repeated on every row of that region. -> Option C
  4. Quick Check:

    FIRST_VALUE repeats first row's value per partition [OK]
Quick Trick: FIRST_VALUE repeats first sorted value per group [OK]
Common Mistakes:
  • Thinking it returns last value instead
  • Assuming it sums or aggregates
  • Ignoring PARTITION BY effect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes