N033-H3 Tier 3 · Intermediate · hard ecommerce · Brightlane

Return both the day-of-week number for that date and the start of its calendar week in a single row

Part of Date Truncation and Extraction in SQL

The problem

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 is 0 and Saturday is 6) and week_start (typed as a timezone-naive timestamp set to the Monday that begins the week).
Schema · ecommerce 5 tables
categories
id integer
name text
parent_id? integer
products
id integer
name text
category_id integer
price numeric
stock_qty integer
attributes? jsonb
order_items
id integer
order_id integer
product_id integer
quantity integer
unit_price numeric
customers
id integer
name text
email text
city? text
country text
created_at timestamptz
is_active boolean
orders
id integer
customer_id integer
ordered_at timestamptz
status text
total_amount numeric

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution Try it yourself first
Solution query
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_week returns the day-of-week index as the number 0. The ::timestamp cast resolves the literal as a timezone-naive datetime; the time-of-day defaults to midnight since the literal carries no clock component, and dow reads the same value regardless.
  • DATE_TRUNC('week', '2024-01-07'::timestamp) AS week_start returns the start of the calendar week the date belongs to. January 7 is a Sunday, the last day of an ISO week, and DATE_TRUNC('week', ...) rounds down to the Monday six days earlier: January 1.
  • There is no FROM because 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.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.

Practice, feedback, mastery, review. That's the loop that turns reading into real skill.

Start free

No account, no credit card. Start solving in under a minute.