Calculate Running Totals with SUM OVER
📖 Scenario: You work at a small bookstore that keeps track of daily sales. You want to see how sales add up day by day to understand the total revenue over time.
🎯 Goal: Create a SQL query that calculates the running total of sales amounts by date using the SUM OVER window function.
📋 What You'll Learn
Create a table called
sales with columns sale_date (date) and amount (integer).Insert the exact sales data for these dates and amounts: 2024-01-01 (100), 2024-01-02 (150), 2024-01-03 (200), 2024-01-04 (50), 2024-01-05 (300).
Write a query that selects
sale_date, amount, and a running total column called running_total.Use the
SUM(amount) OVER (ORDER BY sale_date) syntax to calculate the running total.💡 Why This Matters
🌍 Real World
Running totals help businesses track cumulative sales, expenses, or other metrics over time to understand trends and progress.
💼 Career
Knowing how to use window functions like SUM OVER is valuable for data analysts and database developers to write efficient and insightful queries.
Progress0 / 4 steps