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

transactions: 10 of 200 rows
transaction_iduser_idproduct_idquantitytotal_amounttransaction_date
10671971001223.462026-02-02
11342941050336.932026-03-03
12013911099450.42026-04-04
12684881148563.872026-05-05
13355851197002026-06-06
14026821246290.812026-07-07
146977912953104.282026-08-08
153687613444117.752026-09-09
160397313935-452026-01-10
1670107014421144.692026-02-11

Expected output

First 5 of 12 rows
week_starttotal_quantity
2026-01-0412
2026-01-1110
2026-01-185
2026-01-2511
2026-02-016

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

Weekly aggregation with Sunday-start bucketing
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

Common follow-up questions