Given a table sales with columns sale_date and amount, which query correctly calculates the running total of amount ordered by sale_date?
SELECT sale_date, amount, SUM(amount) OVER (ORDER BY sale_date) AS running_total FROM sales;
Think about how to accumulate sums in order of dates.
Option C uses SUM() OVER (ORDER BY sale_date) which calculates a running total ordered by date. Option C partitions by date, so it sums amounts per date but does not accumulate. Option C groups and sums per date but does not produce running totals. Option C sums over all rows without order, so running total is the same for all rows.
Which option contains a syntax error when trying to calculate a running total of amount ordered by sale_date?
Check the syntax of the frame clause after ORDER BY.
Option B is invalid because the frame clause syntax is incomplete. It should be ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. Options A and B use correct frame syntax. Option B uses default frame which is valid.
You have a large sales table and want to calculate running totals by sale_date. Which query is most efficient?
Consider how window functions perform compared to subqueries.
Option D uses a window function which is optimized for running totals. Option D uses a correlated subquery which is slow for large data. Option D groups sums but does not produce running totals. Option D partitions by date, which is unnecessary and incorrect for running totals.
A query calculates running totals but the totals reset unexpectedly. Which option explains why?
SELECT sale_date, amount, SUM(amount) OVER (PARTITION BY sale_date ORDER BY sale_date) AS running_total FROM sales;
Think about what PARTITION BY does in window functions.
PARTITION BY divides rows into groups and restarts the window function calculation for each group. Here, partitioning by sale_date causes running totals to reset for each date. To get a continuous running total, omit PARTITION BY.
Which frame clause correctly defines a running total that includes all rows from the start up to the current row ordered by sale_date?
Consider how frame boundaries define the window of rows included.
Option A includes all rows from the start (unbounded preceding) up to the current row, which is the definition of a running total. Option A includes current row to the end, which is a running sum backwards. Option A includes all rows, so no running effect. Option A is invalid because unbounded following cannot come before current row.