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 TEXT
| 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
Why this problem exists in real interviews
This tests date truncation combined with a date range filter and aggregation. Interviewers check whether you can compute weekly buckets starting on Sunday and filter to a specific quarter.
Break down the requirements
Step 1: Filter to Q1
Filter transactions to Q1 of 2026: transaction_date >= '2026-01-01' AND < '2026-04-01' (half-open range, no time-of-day boundary issues since transaction_date is a plain date).
Step 2: Truncate to week starting Sunday
Map each transaction to the Sunday that STARTS its week: date(transaction_date, '-' || strftime('%w', transaction_date) || ' days'). strftime('%w', d) is 0 for Sunday..6 for Saturday, so subtracting that many days rolls back to the week's Sunday (a Sunday maps to itself).
Step 3: Sum quantity per week
Group by that week_start and SUM(quantity) to get total transaction quantity per week.
The solution
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_startCommon follow-up questions
- How would you compute week-over-week growth? (Use LAG on the weekly totals: `current_week` - LAG(`total_quantity`) OVER (ORDER BY `week_start`).)
- What if you needed daily granularity instead of weekly? (Remove the date truncation and GROUP BY `transaction_date` directly.)
- How would you handle weeks that span the quarter boundary? (The date filter truncates mid-week; discuss whether to include or exclude partial weeks.)