Brightlane's scheduling system needs to determine whether a proposed interview date falls on a weekend and identify the work week it belongs to. The proposed date is '2024-01-07'.
Write a query to return both the day-of-week number for that date and the start of its calendar week in a single row.
Output:
- A single row with columns
day_of_week(where Sunday is0and Saturday is6) andweek_start(typed as a timezone-naive timestamp set to the Monday that begins the week).
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
EXTRACT(
dow
FROM
'2024-01-07'::TIMESTAMP
) AS day_of_week,
DATE_TRUNC('week', '2024-01-07'::TIMESTAMP) AS week_start The shape
EXTRACT and DATE_TRUNC answer two different questions about the same datetime, and the scheduling system needs both. EXTRACT(dow FROM '2024-01-07') returns 0, marking the date as a Sunday, and DATE_TRUNC('week', '2024-01-07') returns '2024-01-01 00:00:00', the Monday that starts the calendar week the date belongs to. One value is a numeric component, one is a period boundary, and both come off the same source.
Clause by clause
SELECT EXTRACT(dow FROM '2024-01-07'::timestamp) AS day_of_weekreturns the day-of-week index as the number0. The::timestampcast resolves the literal as a timezone-naive datetime; the time-of-day defaults to midnight since the literal carries no clock component, anddowreads the same value regardless.DATE_TRUNC('week', '2024-01-07'::timestamp) AS week_startreturns the start of the calendar week the date belongs to. January 7 is a Sunday, the last day of an ISO week, andDATE_TRUNC('week', ...)rounds down to the Monday six days earlier: January 1.- There is no
FROMbecause both expressions evaluate against the same literal.
Why both functions on the same datetime and not just one
The two outputs serve different downstream uses. The dow value drives the weekend check directly: a value of 0 or 6 flags the date as a weekend. The week_start value identifies the work week the interview belongs to, which the scheduling system uses for capacity lookups. Neither function on its own answers both questions: DATE_TRUNC produces a boundary timestamp that doesn't expose the day-of-week index, and EXTRACT(dow ...) produces a number with no connection to the surrounding week. Pairing them is the standard shape for any calendar-aware scheduling logic.
The trap
January 7, 2024 is a Sunday (dow = 0), but DATE_TRUNC('week', ...) rounds it down to Monday, January 1, six days earlier, not to itself. A reader who assumes the week boundary is the same date as the input will misread the result as off by a week. PostgreSQL's week starts on Monday, so a Sunday input belongs to the week that began the previous Monday. The check is straightforward: when dow returns 0, the corresponding week_start is always six days earlier than the input date, not the input date itself.
You practiced pairing EXTRACT(dow FROM ...) with DATE_TRUNC('week', ...) — extract a numeric component for one purpose and truncate to a period boundary for another, both off the same datetime.