Bird
0
0

Which of the following SQL queries correctly uses a window function to calculate a running total of sales per employee?

easy📝 Syntax Q12 of 15
SQL - Window Functions Fundamentals
Which of the following SQL queries correctly uses a window function to calculate a running total of sales per employee?
ASELECT employee_id, sale_amount, SUM(sale_amount) OVER (ORDER BY sale_date) AS running_total FROM sales GROUP BY employee_id;
BSELECT employee_id, sale_amount, SUM(sale_amount) GROUP BY employee_id ORDER BY sale_date AS running_total FROM sales;
CSELECT employee_id, sale_amount, SUM(sale_amount) FROM sales WINDOW (PARTITION BY employee_id ORDER BY sale_date) AS running_total;
DSELECT employee_id, sale_amount, SUM(sale_amount) OVER (PARTITION BY employee_id ORDER BY sale_date) AS running_total FROM sales;
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct window function syntax

    SELECT employee_id, sale_amount, SUM(sale_amount) OVER (PARTITION BY employee_id ORDER BY sale_date) AS running_total FROM sales; uses SUM() OVER (PARTITION BY ... ORDER BY ...), which is the correct syntax for running totals per employee.
  2. Step 2: Check other options for syntax errors

    Options B, C, and D misuse GROUP BY or WINDOW clauses incorrectly, causing syntax errors or wrong logic.
  3. Final Answer:

    The correct query: SELECT employee_id, sale_amount, SUM(sale_amount) OVER (PARTITION BY employee_id ORDER BY sale_date) AS running_total FROM sales; -> Option D
  4. Quick Check:

    Window function syntax = SELECT employee_id, sale_amount, SUM(sale_amount) OVER (PARTITION BY employee_id ORDER BY sale_date) AS running_total FROM sales; [OK]
Quick Trick: Window functions use OVER(), GROUP BY does not [OK]
Common Mistakes:
  • Using GROUP BY with window function syntax
  • Misplacing WINDOW clause
  • Omitting OVER() for window functions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes