CMD Guide
HomeDatabasesSQL Practice Problems

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.

playergenderdayscore_points
PriyankaF2019-12-3017
PriyaF2019-12-3123
AronF2020-01-0117
AliceF2020-01-0723

Expected for the F team — each day is the sum of that day and all earlier days:

genderdaytotal= running sum
F2019-12-301717
F2019-12-314017+23
F2020-01-015740+17
F2020-01-078057+23
Running total for the female team accumulating day by day, with the window-function frame UNBOUNDED PRECEDING to CURRENT ROW
Running total for the female team accumulating day by day, with the window-function frame UNBOUNDED PRECEDING to CURRENT ROW

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

Takeaways


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.

🎨 Explain it visually

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.
🤔 Walk me through it (interactive)

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.
🧪 Quiz me & fix my gaps

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.
🧠 Make it stick

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.

📝 My notes