March Revenue by Customer

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

Problem

How much has each customer spent in March, counting every March in the transactions table rather than just this year's? Finance's seasonal review lists each user ID with that combined March total, biggest spender first.

One month, every customer, every dollar accounted for.

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 10 rows
user_idmarch_total
2941929.24
9731878.8400000000001
8761649.85
12641555.5600000000002
1001434.33

Worked solution and explanation

What this really is

Strip off the finance costume and this is a one-month slice folded into a per-customer sum, with a twist in the slice: March means every March in transactions, 2022 through 2026, added together. Everyone writes the grouping. What separates candidates is reading the window right. Pin March to 2026 out of habit and the answer shrinks from ten customers to six; forget to sum and you hand back twenty purchase lines instead of ten totals.

The trap

The reflex is to read 'March revenue' as this March. On this table that is a different question: March rows exist in five years, and four customers (973, 1264, 391 and 682) only bought in older Marches. The preview gives it away if you look: 973 sits second at 1878.84, yet has no March purchase in 2026 at all. Reconcile the preview against the data before you commit to a window.

March pinned to one year
WHERE transaction_date >= '2026-03-01' AND transaction_date < '2026-04-01' returns six customers. 973, 1264, 391 and 682 drop out because their March purchases fell in earlier years.
Every March pooled
WHERE strftime('%m', transaction_date) = '03' keeps the twenty March rows from all five years, so all ten customers appear with their combined spend.

Step 1: Keep every March row

WHERE strftime('%m', transaction_date) = '03' keeps March rows from every year and drops the rest before any summing runs, so the total only ever sees March money. There is deliberately no year predicate.

Step 2: Fold purchases into one row per customer

GROUP BY user_id with SUM(total_amount) turns the twenty March purchases into ten totals. Nine of the ten customers bought more than once, so skipping this step is never a near miss: 294 alone has three March lines that add to 1929.24.

Step 3: Order the standings

ORDER BY march_total DESC puts the biggest spender on top. Sorting on the alias you return keeps the order you are graded on visible in the output itself.

Canonical solution
SELECT user_id,
       SUM(total_amount) AS march_total
FROM transactions
WHERE strftime('%m', transaction_date) = '03'
GROUP BY user_id
ORDER BY march_total DESC

Keep every March row, sum per user_id, biggest march_total first.

Common follow-up questions