The register, day by day
- O(n²) · Very hard
- Full plan
- SQL
- window functions
- grouping
- conditionals
Problem
The owner wants to watch the money add up: how much came in on each day that had orders, and how much the store had taken in so far by the end of that day.
Today is January 15, 2026, and nobody orders in the future. An order dated after today is one of today's with the day mistyped, and its money goes with today's.
Show the day, that day's money, and the running total, in that order, with both amounts rounded to 2 decimals and the most recent day on top. You can write today as '2026-01-15' or as DATE('now').
What it has to return
These rows, in this order
| day | day_total | running_total |
|---|---|---|
| 2026-01-15 | 499.7 | 2289.85 |
| 2026-01-14 | 899 | 1790.15 |
| 2026-01-12 | 35.65 | 891.15 |
| 2026-01-11 | 378 | 855.5 |
| 2026-01-10 | 45.5 | 477.5 |
| 2026-01-09 | 52.5 | 432 |
| 2026-01-04 | 288.5 | 379.5 |
| 2026-01-03 | 91 | 91 |
This is what comes out with the data in the practice database this challenge uses. The column names don’t count, so name yours however you like: what counts is how many there are and the order they come in. Besides this one, the challenge has hidden tests with other data that are revealed when you submit your solution.
You start with this
SQL
SELECT o.placed,
SUM(p.price * o.quantity)
AS money
FROM orders o
JOIN products p
ON o.product_id = p.id
GROUP BY o.placed;It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.
More O(n²) challenges
- The third stampSQL · window functions · joins · dates
- The week of the 4thSQL · dates · grouping · strings
- What went out todaySQL · changing data · subqueries · nulls
- Which day do we close?SQL · dates · grouping · conditionals
- You started with a KeyboardSQL · window functions · joins
- A line for every daySQL · recursion · dates · window functions