Running Total for Different Genders
Read the spec carefully: this is a running total, not a daily total
The wording "find the total score for each gender on each day" is easy to misread as "sum the scores that happened on that day". The expected output proves it means the cumulative (running) total up to and including that day. That ambiguity is exactly what the original lesson conflated, so let's pin it with data.
| player | gender | day | score_points |
|---|---|---|---|
| Priyanka | F | 2019-12-30 | 17 |
| Priya | F | 2019-12-31 | 23 |
| Aron | F | 2020-01-01 | 17 |
| Alice | F | 2020-01-07 | 23 |
Expected for the F team — each day is the sum of that day and all earlier days:
| gender | day | total | = running sum |
|---|---|---|---|
| F | 2019-12-30 | 17 | 17 |
| F | 2019-12-31 | 40 | 17+23 |
| F | 2020-01-01 | 57 | 40+17 |
| F | 2020-01-07 | 80 | 57+23 |
The modern answer: a window function
A running total is the textbook use of a window function — it keeps the rows (unlike
GROUP BY, which collapses them) and computes a sum over a moving frame:
SELECT gender,
day,
SUM(score_points) OVER (PARTITION BY gender ORDER BY day) AS total
FROM Scores
ORDER BY gender, day;
How to read it: PARTITION BY gender resets the accumulator for each team;
ORDER BY day defines the running direction. When a window has an ORDER BY and no explicit
frame, the default frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW — i.e. "everything from the
start of this gender up to the current day", which is exactly a running total. One ordered pass over the rows.
The self-join answer, and why it doesn't scale
Before window functions were widespread, the same result came from a self-join:
SELECT s1.gender, s1.day, SUM(s2.score_points) AS total
FROM Scores s1
JOIN Scores s2 ON s2.gender = s1.gender AND s2.day <= s1.day
GROUP BY s1.gender, s1.day
ORDER BY s1.gender, s1.day;
It's correct, but it pairs every row with all rows of the same gender on or before it before grouping — roughly O(n²) work. On thousands of rows per gender that's a quadratic blow-up the window version avoids entirely. Reach for the self-join only on an engine with no window support (old MySQL < 8.0).
Pitfalls
- Ties on the ORDER BY key. The default
RANGEframe lumps all rows with the same day into one cumulative step. If you want strict row-by-row accumulation through ties, useROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. Here(gender, day)is unique so it doesn't bite — but know the difference. - Always
ORDER BYin the outer query too; the window's internal ordering doesn't guarantee the result-set order.
Takeaways
- "Total on each day" + an output that grows = a running total → window function.
SUM(x) OVER (PARTITION BY g ORDER BY d)defaults to a cumulative frame; that is the running total.- The self-join equivalent is O(n²) — fine for tiny inputs, wrong tool at scale.
Re-authored for this guide: the prior version mis-stated the spec and showed an unordered intermediate table. Pattern: LeetCode 1308. Window-frame semantics per the PostgreSQL & SQL-standard window-function docs.
🤖 Don't fully get this? Learn it with Claude
Stuck on Running Total for Different Genders? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Build the mental picture, not memorization.
I just read a lesson on **Running Total for Different Genders** (Databases) and want to truly understand it. Explain Running Total for Different Genders from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Socratic — adapts to where you're stuck.
Teach me **Running Total for Different Genders** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Active recall exposes what you missed.
Quiz me on **Running Total for Different Genders** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Intuition + hook + flashcards for long-term memory.
Help me remember **Running Total for Different Genders** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.