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

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

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.

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
The Monday-based shortcut
-- 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