Best day yet
- O(n²) · Very hard
- Full plan
- SQL
- window functions
- grouping
- subqueries
Problem
"Best day we've ever had," the owner says after closing up on a good one, and wants to know how many times that has actually been true. A day sets a record when its money (each product's price times the units ordered) beats the money of every single day before it. The first day with orders counts: it had nothing to beat. Tying the record isn't breaking it.
Show two columns, the day and then its money, rounded to 2 decimals, with the record that still stands on top and the oldest one at the bottom. Orders dated up to today, January 15, 2026, count. The one dated the 20th stays out: a day that hasn't come yet doesn't beat anything.
What it has to return
These rows, in this order
| set_on | money |
|---|---|
| 2026-01-14 | 899 |
| 2026-01-11 | 378 |
| 2026-01-04 | 288.5 |
| 2026-01-03 | 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 AS day,
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
- First with lastSQL · window functions · joins · grouping
- How soon they're backSQL · window functions · dates · grouping
- January off the shelfSQL · dates · nulls · grouping
- Lighthouse, TheSQL · strings · conditionals · sorting
- One raise, not twoSQL · changing data · conditionals
- Out of every 100 in payrollSQL · window functions · decimals