Bird
0
0

Given the table sales with columns 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 month and revenue, what will this query return?
SELECT month, revenue, LAG(revenue) OVER (ORDER BY month) AS prev_revenue FROM sales;
AEach row shows only current month revenue
BEach row shows current month revenue and next month's revenue
CEach row shows current month revenue and previous month's revenue
DQuery will cause a syntax error
Step-by-Step Solution
Solution:
  1. Step 1: Understand LAG with ORDER BY month

    The LAG(revenue) returns the revenue from the previous month ordered by month.
  2. Step 2: Interpret query output

    Each row shows current month revenue and previous month's revenue in prev_revenue. The first row's prev_revenue will be NULL.
  3. Final Answer:

    Each row shows current month revenue and previous month's revenue -> Option C
  4. Quick Check:

    LAG = previous row value [OK]
Quick Trick: LAG returns previous row's value based on ORDER BY [OK]
Common Mistakes:
  • Confusing LAG with LEAD
  • Expecting next month's revenue instead
  • Thinking query causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes