Using a correlated query:


  SELECT t.id,
         t.count,
         (SELECT SUM(x.count)
            FROM TABLE x
           WHERE x.id <= t.id) AS cumulative_sum
    FROM TABLE t
ORDER BY t.id

Using MySQL variables:


  SELECT t.id,
         t.count,
         @running_total := @running_total + t.count AS cumulative_sum
    FROM TABLE t
    JOIN (SELECT @running_total := 0) r
ORDER BY t.id

Note:

  • The JOIN (SELECT @running_total := 0) r is a cross join, and allows for variable declaration without requiring a separate SET command.
  • The table alias, r, is required by MySQL for any subquery/derived table/inline view

Caveats:

  • MySQL specific; not portable to other databases
  • The ORDER BY is important; it ensures the order matches the OP and can have larger implications for more complicated variable usage (IE: psuedo ROW_NUMBER/RANK functionality, which MySQL lacks)
Answer from OMG Ponies on Stack Overflow
🌐
PopSQL
popsql.com › learn-sql › mysql › how-to-calculate-cumulative-sum-running-total-in-mysql
How to Calculate Cumulative Sum-Running Total in MySQL - PopSQL
Before MySQL version 8 you can use variables for this: SELECT t.day, t.rental_count, @running_total:=@running_total + t.rental_count AS cumulative_sum FROM ( SELECT date(rental_date) as day, count(rental_id) as rental_count FROM rental GROUP BY day ) t JOIN (SELECT @running_total:=0) r ORDER BY t.day; day | rental_count | cumulative_sum -----------+--------------+---------------- 2005-05-24 | 8 | 8 2005-05-25 | 137 | 145 2005-05-26 | 174 | 319 2005-05-27 | 166 | 485 2005-05-28 | 196 | 681
Discussions

sql - Creating a cumulative sum column in MySQL - Stack Overflow
Sample table ID: (num is a key so there wouldn't be any duplicates) num 1 5 6 8 2 3 Desired output: (Should be sorted and have a cumulative sum column) num cumulative 1 1 2 3 3 6 5 11... More on stackoverflow.com
🌐 stackoverflow.com
Cumulative Sum in MySQL
Cumulative Sums are a little tricky in MySQL. The simplest way to tackle this would be to use the running_total measure, or utilize the running_total() function in Table Calculations. If you need to be able to reference your running total or sum in other fields, these approaches won’t do it. More on community.looker.com
🌐 community.looker.com
5
0
September 5, 2015
sql - How to calculate cumulative sums in MySQL - Stack Overflow
I am preparing for interviews and came across this question while practicing some SQL questions recently asked in Amazon. I could not find the table though, but the question is as follows: Find the More on stackoverflow.com
🌐 stackoverflow.com
February 21, 2022
Cumulative sum with mysql - Stack Overflow
It works almost ok, I would like to have as result set a date and amount of cumulative sum as uniqueClicks, the problem is that in my result set it is not added up together. More on stackoverflow.com
🌐 stackoverflow.com
🌐
TutorialsPoint
tutorialspoint.com › how-to-create-a-cumulative-sum-column-in-mysql
How to create a Cumulative Sum Column in MySQL?
mysql> create table CumulativeSumDemo −> ( −> BookId int, −> BookPrice int −> ); Query OK, 0 rows affected (0.67 sec)
🌐
W3Schools
w3schools.com › sql › func_mysql_sum.asp
MySQL SUM() Function
String Functions: ASCII CHAR_LENGTH CHARACTER_LENGTH CONCAT CONCAT_WS FIELD FIND_IN_SET FORMAT INSERT INSTR LCASE LEFT LENGTH LOCATE LOWER LPAD LTRIM MID POSITION REPEAT REPLACE REVERSE RIGHT RPAD RTRIM SPACE STRCMP SUBSTR SUBSTRING SUBSTRING_INDEX TRIM UCASE UPPER Numeric Functions: ABS ACOS ASIN ATAN ATAN2 AVG CEIL CEILING COS COT COUNT DEGREES DIV EXP FLOOR GREATEST LEAST LN LOG LOG10 LOG2 MAX MIN MOD PI POW POWER RADIANS RAND ROUND SIGN SIN SQRT SUM TAN TRUNCATE Date Functions: ADDDATE ADDTIME CURDATE CURRENT_DATE CURRENT_TIME CURRENT_TIMESTAMP CURTIME DATE DATEDIFF DATE_ADD DATE_FORMAT DA
🌐
Datareportive
datareportive.com › tutorial › mysql › how-to-calculate-cumulative-sum-running-total
How to Calculate Cumulative Sum-Running Total in MySQL | DataReportive Tutorials
The most efficient way to calculate a running total in MySQL is by using the SUM() window function in combination with the OVER() clause. Here's an example query that calculates the cumulative sum of sale_amount for each day:
🌐
OneUptime
oneuptime.com › home › blog › how to calculate cumulative sums in mysql
How to Calculate Cumulative Sums in MySQL
March 31, 2026 - Cumulative sums in MySQL 8.0+ are best expressed with SUM() OVER (PARTITION BY ... ORDER BY ...) which is readable, performant, and correct for all edge cases. For MySQL 5.7 and earlier, use user variables that accumulate across rows ordered ...
🌐
Coffingdw
coffingdw.com › mysql-analytics-cumulative-sum
MySQL Analytics – Cumulative Sum – Software connecting all databases
The query below sorts the dataset rows by product_id (major sort) and sale_date (minor sort) and then adds up the daily_sales from the first row to the last. I want you to focus on the importance of the ORDER BY statement, which is ORDER BY product_id and then sale_date.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › mysql › how-to-compute-a-running-total-in-mysql
How to Compute a Running Total in MySQL - GeeksforGeeks
July 23, 2025 - A running total also known as a cumulative sum represents the summation of the values as they accumulate over time or a specific sequence. It offers insights into the cumulative progress, growth, or accumulation of the quantity over a period.
🌐
Interview Query
interviewquery.com › p › sql-cumulative-sum-guide
SQL Cumulative SUM: Window Functions, Rolling Totals & Best Practices
March 17, 2026 - Learn how to calculate a cumulative sum (running total) in SQL using window functions, self-joins, and subqueries. Includes examples, FAQs, and interview-style practice.
🌐
myCompiler
mycompiler.io › view › D0yhJVX9z8U
cumulative sum (MySQL) - myCompiler
July 3, 2024 - -- create a table CREATE TABLE students ( name TEXT , score INTEGER -- ,gender TEXT NOT NULL ); -- insert some values INSERT INTO students VALUES ('a', 5); INSERT INTO students VALUES ('x', 10); INSERT INTO students VALUES ('d', 2); INSERT INTO students VALUES ('m', 5); -- fetch some values with row_table as (SELECT * , ROW_NUMBER() OVER (ORDER BY name) as row_num FROM students) select r1.name, r1.score, sum(r2.score) as cum_sum from row_table r1 join row_table r2 on r1.row_num >= r2.row_num group by r1.name,r1.score order by cum_sum;
🌐
OneUptime
oneuptime.com › home › blog › how to use running totals with sum() over() in mysql
How to Use Running Totals with SUM() OVER() in MySQL
March 31, 2026 - Compute the cumulative total sales across all categories ordered by date. SELECT sale_date, product_category, amount, SUM(amount) OVER (ORDER BY sale_date, id) AS running_total FROM daily_sales ORDER BY sale_date, id;
🌐
CastorDoc
castordoc.com › how-to › how-to-calculate-cumulative-sum-running-total-in-mysql
How to Calculate Cumulative Sum/Running Total in MySQL?
To perform calculations and operations in MySQL, a variety of functions and commands are available. These enable us to manipulate data, perform mathematical computations, and aggregate values. Some commonly used functions in MySQL include SUM, COUNT, AVG, MAX, and MIN. These functions are integral to calculating cumulative sums and running totals.
🌐
W3Schools
w3schools.com › sql › sql_sum.asp
SQL SUM() Function
The SUM() function is used to calculate the total sum of values within a numeric column.
🌐
Looker
community.looker.com › q&a
Cumulative Sum in MySQL - Looker - Google Developer forums
September 5, 2015 - Cumulative Sums are a little tricky in MySQL. The simplest way to tackle this would be to use the running_total measure, or utilize the running_total() function in Table Calculations. If you need to be able to referenc…
🌐
GitHub
gist.github.com › MagePsycho › 431233c15e1e0d1532376dcc1b8102f0
MySQL: Running Total (Cumulative Sum) · GitHub
MySQL: Running Total (Cumulative Sum). GitHub Gist: instantly share code, notes, and snippets.
🌐
Stack Overflow
stackoverflow.com › questions › 71202242 › how-to-calculate-cumulative-sums-in-mysql
sql - How to calculate cumulative sums in MySQL - Stack Overflow
February 21, 2022 - SELECT t.day, t.product_count, @running_total:=@running_total + t.product_count AS cumulative_sum FROM ( SELECT date(purchase_date) as day, count(product_id) as product_count FROM products where day > DATE_SUB(now(), INTERVAL 6 MONTH) AND customer_city = 'Seattle' GROUP BY day ORDER BY product_count desc) t JOIN (SELECT @running_total:=0) r ORDER BY t.day LIMIT 10;
🌐
LabEx
labex.io › tutorials › mysql-how-to-calculate-cumulative-totals-in-mysql-418610
How to calculate cumulative totals in MySQL | LabEx
-- Create a sample sales table ... 75.25); -- Basic cumulative total calculation SELECT sale_date, amount, SUM(amount) OVER (ORDER BY sale_date) AS cumulative_total FROM sales; ... By understanding these basics, database ...
🌐
Five
five.co › blog › sql-cumulative-sum
SQL Cumulative Sum: A Practical Guide | Five
May 4, 2026 - Sign up for free access to Five’s online development environment and start building your MySQL web application today. Build Your Database In 3 Steps Start Developing Today ... Cumulative sums are just the beginning. Once you’re comfortable with window functions, you can use similar techniques for other calculations.