Weekly Transaction Volume
An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.
- Domain
- SQL
- Difficulty
- easy
- Seniority
- L3
- Asked in
- Amazon
Problem
In transactions, how many units (quantity) moved in each week of Q1 2026, where a week runs Sunday through Saturday and is labelled by the date of the Sunday that opens it? The weeks read as a timeline, earliest first.
Weekly volume. The pulse.
Tables
transactions
Columns: transaction_id INTEGER, user_id INTEGER, product_id INTEGER, quantity INTEGER, total_amount REAL, transaction_date DATE
| transaction_id | user_id | product_id | quantity | total_amount | transaction_date |
|---|---|---|---|---|---|
| 1067 | 197 | 1001 | 2 | 23.46 | 2026-02-02 |
| 1134 | 294 | 1050 | 3 | 36.93 | 2026-03-03 |
| 1201 | 391 | 1099 | 4 | 50.4 | 2026-04-04 |
| 1268 | 488 | 1148 | 5 | 63.87 | 2026-05-05 |
| 1335 | 585 | 1197 | 0 | 0 | 2026-06-06 |
| 1402 | 682 | 1246 | 2 | 90.81 | 2026-07-07 |
| 1469 | 779 | 1295 | 3 | 104.28 | 2026-08-08 |
| 1536 | 876 | 1344 | 4 | 117.75 | 2026-09-09 |
| 1603 | 973 | 1393 | 5 | -45 | 2026-01-10 |
| 1670 | 1070 | 1442 | 1 | 144.69 | 2026-02-11 |
Expected output
| week_start | total_quantity |
|---|---|
| 2026-01-04 | 12 |
| 2026-01-11 | 10 |
| 2026-01-18 | 5 |
| 2026-01-25 | 11 |
| 2026-02-01 | 6 |
Worked solution and explanation
What this really is
This is a calendar-alignment problem wearing a sales dashboard. Summing quantity is the easy half. What separates candidates is snapping each transaction_date back to the Sunday that opens its week and printing that Sunday as the label. SQLite has no Sunday-start week function, so the snap has to be written as plain date arithmetic. Reach for strftime('%W', ...) instead and every Sunday sale slides into the week before it. On this seed the 2026-01-11 and 2026-01-18 sales land in the wrong bucket, and the totals stop matching.
Step 1: Filter on the transaction date, not the label
transaction_date holds 'YYYY-MM-DD' text, so >= '2026-01-01' and < '2026-04-01' is an exact half-open Q1 range with no time-of-day edge. Putting the range on transaction_date instead of week_start matters in a year where January 1 is mid-week: those first days belong to a week whose Sunday falls in December, and they still count.
Step 2: Snap each date back to its Sunday
strftime('%w', transaction_date) is 0 on Sunday through 6 on Saturday. Subtracting that many days with date(transaction_date, '-' || strftime('%w', transaction_date) || ' days') rolls every date back to the Sunday that opens its week, and a Sunday maps to itself. The result is a real date, which is the label the output wants.
Step 3: Sum per week, earliest first
GROUP BY week_start with SUM(quantity) AS total_quantity gives one row per week. ORDER BY week_start reads as a timeline with no help, because ISO date text sorts chronologically.
SELECT date(transaction_date, '-' || strftime('%w', transaction_date) || ' days') AS week_start,
SUM(quantity) AS total_quantity
FROM transactions
WHERE transaction_date >= '2026-01-01' AND transaction_date < '2026-04-01'
GROUP BY week_start
ORDER BY week_start-- Looks right, but %W counts weeks that start on Monday
SELECT strftime('%Y-%W', transaction_date) AS week_start,
SUM(quantity) AS total_quantity
FROM transactions
WHERE transaction_date >= '2026-01-01' AND transaction_date < '2026-04-01'
GROUP BY week_start
ORDER BY week_start%W opens weeks on Monday and returns a number, so Sunday sales join the previous week.
Common follow-up questions
- How would you compute week-over-week growth? (Apply `LAG(total_quantity) OVER (ORDER BY week_start)` to the weekly result and subtract it from the current total.)
- What if you needed daily granularity instead of weekly? (Drop the `week_start` derivation and group on `transaction_date` directly.)
- How would you handle weeks that span the quarter boundary? (Decide whether a partial first or last week is reported as is, excluded, or widened to a full 7 days. Each choice changes which rows the filter keeps.)