The week of the 4th
- O(n²) · Very hard
- Full plan
- SQL
- dates
- grouping
- strings
Problem
The store closes its books by the week, Sunday through Saturday, and in the notebook by the register each week goes by its Sunday: the one that runs from January 4 to 10 is "the week of the 4th," whether anything came in that Sunday or not. The owner wants the page with every week that had orders.
Show three columns: the date of each week's Sunday, how many orders the week had, and its money (each product's price times the units ordered, rounded to 2 decimals). The most recent week goes on top.
The '%w' format of STRFTIME gives the day of the week as text from '0' to '6', and '0' is Sunday. And the '-1 days' that DATE takes is text like any other: you can also glue it together with ||.
Orders dated up to today, January 15, 2026, count. The one dated the 20th would land in a week that hasn't started yet, and it stays out.
What it has to return
These rows, in this order
| sunday | made | money |
|---|---|---|
| 2026-01-11 | 6 | 1492.35 |
| 2026-01-04 | 4 | 386.5 |
| 2025-12-28 | 1 | 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 placed,
STRFTIME('%w', placed) AS num,
DATE(placed, '-1 days')
AS day_before
FROM orders;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
- 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
- Arriving WednesdaySQL · dates · conditionals
- Best day yetSQL · window functions · grouping · subqueries