Brightlane's weekly engagement digest groups user activity to the start of each calendar week.
Write a query to return the week boundary produced by truncating the timestamp '2024-03-15 14:32:07' to week precision.
Output:
- A single row with one column,
week_start, typed as a timezone-naive timestamp.
Schema · ecommerce 5 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
DATE_TRUNC('week', '2024-03-15 14:32:07'::TIMESTAMP) AS week_start The shape
DATE_TRUNC('week', ...) rounds a datetime down to the Monday that starts its ISO calendar week and returns a datetime. The March 15 timestamp, a Friday, lands on '2024-03-11 00:00:00', the Monday three days earlier. That Monday boundary is what makes every weekday inside the same week collapse to a shared value.
Clause by clause
SELECT DATE_TRUNC('week', '2024-03-15 14:32:07'::timestamp)evaluates the truncation against a single literal. The::timestampcast resolves the string as a timezone-naive datetime, and the truncation preserves the input type, so the result is also atimestamp. The time-of-day component on the input is discarded; only the calendar position matters for finding the week boundary.AS week_startlabels the column as the start-of-week value the engagement digest groups around.
The trap
PostgreSQL's week starts on Monday, not Sunday. A learner expecting the Sunday-based convention used by spreadsheet tools and some calendar apps will see the truncation land one day later than they expected and misread it as a bug. The Monday-start rule is the ISO standard and is fixed: there is no field name that flips DATE_TRUNC('week', ...) to a Sunday start. Any consumer that needs a Sunday-start week has to compute the offset separately, not rely on the field name.
You practiced DATE_TRUNC('week', ...) — every datetime within the same calendar week resolves to the same Monday-start boundary.