SQL Running Total: How To Calculate a Running Total in SQL

Channel Avatar
SQL Running Total: How To Calculate a Running Total in SQL
SQL Running Total: How To Calculate a Running Total in SQL
Calculating a running total in SQL is something that we need to do sometimes.

A running total is a number that includes all of the previous numbers before it. It can be used to calculate sales so far for a month, or the score so far in a sports match.

You can use the concept of a “window function” in SQL to calculate the running total. This works in major SQL vendors including Oracle, SQL Server, MySQL, and PostgreSQL.

Watch this video to see how to do it: both calculating a running total and a running total for separate groups.

The script used for the sample data is available here: https://dbshostedfiles.s3.us-west-2.amazonaws.com/dbs/create_sales_history.sql

Blog post: https://www.databasestar.com/sql-running-total/

00:00 Intro
00:20 What is a running total?
00:40 Sample table
00:57 Select query
02:00 Add the SUM function
03:29 Results
04:03 Grouping and running total
05:32 Summary

Get my free SQL Cheat Sheets: https://www.databasestar.com/get-sql-cheat-sheets/
SQL Roadmap from basics to advanced: https://www.databasestar.com/sql-roadmap/
️ Learn how to design a database: https://databasestar.mykajabi.com/edd
Improve your SQL and database skills with Database Star Academy: https://www.databasestar.com/dsa/
Contact me here: https://www.databasestar.com/contact/

Database Design Exercises: https://bbrumm.gumroad.com/l/hqsxi
SQL Practice Questions: https://bbrumm.gumroad.com/l/ulekqk
SQL Performance Quick Guide: https://bbrumm.gumroad.com/l/fcvny
Ultimate SQL How-To Guide: https://bbrumm.gumroad.com/l/oqggau

Take the opportunity to connect and share this video with your friends and family if you find it useful.

Read Also

Leave a Reply

Your email address will not be published. Required fields are marked *