Active User Revenue for April

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

Each row in transactions records a purchase's quantity and total_amount, and a purchase's revenue is the two multiplied together. Return the total revenue from April 2026 purchases made by users whose account in users is active.

Total revenue from active users in a single month

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

users

Columns: user_id INTEGER, username TEXT, email TEXT, signup_date TEXT, account_status TEXT, age_bucket TEXT

users: 10 of 200 rows
user_idusernameemailsignup_dateaccount_statusage_bucket
100alicealice@example.com2025-02-02inactive25-34
197aaron42aaron42@example.com2026-03-03suspended35-44
294 ameliaamelia@example.com2024-04-04pending_verification45-54
391arjun arjun@example.com2025-05-05active55-64
488ava99ava99@example.com2026-06-06inactive65+
585andrew_kandrew_k@example.com2024-07-07suspendedNULL
682anikaanika@example.com2025-08-08 16:34:00pending_verification18-24
779aidenaiden@example.com2026-09-09 14:53:00active25-34
876aria_baria_b@example.com2024-10-10inactive35-44
973brianbrian@example.com2025-11-11suspended45-54

Expected output

First 1 of 1 rows
total_revenue
7878.6

Worked solution and explanation

What this really is

Strip the finance costume and this is a filtered sum whose filter lives in the wrong table. Everything you add up sits on transactions; the one fact that decides which rows count, whether the account is active, sits only on users. Anyone can write a SUM over April. What separates candidates is two quiet mistakes: reading 'quantity times amount' as two totals multiplied together, and skipping the join because the date filter feels like enough. Multiply the totals and the answer is orders of magnitude too large; skip the join and suspended, inactive and still-verifying accounts pad the reconciliation.

Build it in order

Step 1: Bring account_status onto each purchase

Inner-join transactions to users on user_id. Status is a property of the person, not the purchase, so there is nothing to filter on until the two tables meet. The inner join also drops any purchase whose user has no row in users: revenue nobody can vouch for stays out of a reconciliation.

Step 2: Keep only 'active', and only April

u.account_status = 'active' is an exact match; 'inactive', 'suspended' and 'pending_verification' are all out. The month test compares the year-month of transaction_date against '2026-04', pinning year and month together so April of an earlier year in the data never leaks in.

Step 3: Multiply first, then add

SUM(t.quantity * t.total_amount) computes each purchase's revenue and adds those. Sum-of-products and product-of-sums are different numbers, and only the first is revenue. There is no GROUP BY: the ask is one figure.

April revenue from active accounts
SELECT SUM(t.quantity * t.total_amount) AS total_revenue
FROM transactions t
INNER JOIN users u ON t.user_id = u.user_id
WHERE u.account_status = 'active'
  AND strftime('%Y-%m', t.transaction_date) = '2026-04'

One row, one number: the reconciliation total in total_revenue.

Product of totals
SUM(quantity) * SUM(total_amount) pairs every unit from every purchase with every dollar from every other purchase. Even on a month with a handful of rows, the result is a cross-product no ledger will ever match.
Total of products
SUM(quantity * total_amount) keeps each purchase's quantity attached to its own amount before anything is added, so each row contributes exactly its own revenue.
The answer that looks right and is not
SELECT SUM(quantity) * SUM(total_amount) AS total_revenue
FROM transactions
WHERE strftime('%Y-%m', transaction_date) = '2026-04'

Wrong twice: a product of totals, and every account status counted.

Common follow-up questions