Bird
0
0

Which of the following is the correct syntax to calculate a running total of sales using SUM as a window function in PostgreSQL?

easy📝 Syntax Q12 of 15
PostgreSQL - Window Functions in PostgreSQL
Which of the following is the correct syntax to calculate a running total of sales using SUM as a window function in PostgreSQL?
ASELECT sales, SUM(sales) OVER (ORDER BY date) AS running_total FROM sales_data;
BSELECT sales, SUM(sales) FROM sales_data ORDER BY date;
CSELECT sales, SUM(sales) PARTITION BY date FROM sales_data;
DSELECT sales, SUM(sales) OVER PARTITION BY date FROM sales_data;
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct window function syntax

    SUM(sales) OVER (ORDER BY date) calculates cumulative sum ordered by date.
  2. Step 2: Check other options for syntax errors

    The other options misuse window function syntax or omit OVER keyword.
  3. Final Answer:

    SELECT sales, SUM(sales) OVER (ORDER BY date) AS running_total FROM sales_data; -> Option A
  4. Quick Check:

    SUM() OVER (ORDER BY ...) is correct syntax [OK]
Quick Trick: Use OVER() with ORDER BY for running totals [OK]
Common Mistakes:
  • Omitting OVER() clause
  • Using PARTITION BY without OVER()
  • Confusing GROUP BY with window functions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes