Calculate Running Total

Create Table

-- Create table called adventurers
CREATE TABLE adventurers (
    -- integer variable
    id int,
    -- string variable
    name varchar(255),
    -- integer variable
    age int,
    -- string variable
    race varchar(255),
    -- string variable
    weapon varchar(255)
)

Insert Rows

-- Insert into the table adventurers
INSERT INTO adventurers (id, name, age, race, weapon)
VALUES (1, 'Fjoak Doom-Wife', 28, 'Human', 'Axe'),
       (2, 'Alooneric Cortte', 29, 'Elf', 'Bow'),
       (3, 'Piperel Ramsay', 35, 'Elf', 'Sword'),
       (4, 'Casimir Yardley', 14, 'Elf', 'Magic')

Create Running Total

-- Get name, age, and...
SELECT copy1.name, copy1.age, 
-- a sum of age from a copy of adventurers table called copy2 
-- Where the id of the copy2 is less than or equal to the id of copy1
-- and call it running_total
(SELECT sum(copy2.age) FROM adventurers copy2 WHERE copy2.id <= copy1.id) AS running_total
-- From the adventurer's table, calling it copy1  
FROM adventurers copy1
-- Sort by running total
ORDER BY running_total
nameagerunning_total
Fjoak Doom-Wife2828
Alooneric Cortte2957
Piperel Ramsay3592
Casimir Yardley14106