Problem Statement
Explain when to prefer a window function over a GROUP BY for analytical queries.
Explanation
Choose window functions when you need to keep row detail while adding analytics like running totals, ranks, or group totals. Windows compute across related rows but do not collapse the result, so you can show both the raw row and the metric together.
GROUP BY is right when you want one row per group. It summarizes and discards the original granularity. Use it for final aggregates, and windows for per-row insights.
Code Solution
SolutionRead Only
SELECT order_id, amount,
SUM(amount) OVER (PARTITION BY customer_id) AS cust_total
FROM orders;