Bird
0
0

Which of the following is the correct syntax to calculate a running total of sales per department using OVER with PARTITION BY?

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

    The correct syntax places PARTITION BY before ORDER BY inside the parentheses after OVER.
  2. Step 2: Check each option's syntax

    SELECT department, sales, SUM(sales) OVER (PARTITION BY department ORDER BY date) AS running_total FROM sales_data; correctly uses SUM(sales) OVER (PARTITION BY department ORDER BY date). Others misplace PARTITION BY or OVER keywords.
  3. Final Answer:

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

    PARTITION BY before ORDER BY inside OVER [OK]
Quick Trick: Remember: OVER (PARTITION BY ... ORDER BY ...) is the correct order [OK]
Common Mistakes:
  • Swapping PARTITION BY and ORDER BY order
  • Placing PARTITION BY outside OVER parentheses
  • Using PARTITION BY without OVER

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes