# Active User Revenue for April

> Total revenue from active users in a single month

Canonical URL: <https://datadriven.io/problems/active_user_revenue_for_april>

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

The finance team is reconciling April 2026 revenue and needs a total that only includes transactions from users with active accounts. Revenue for a transaction is calculated as quantity multiplied by the transaction amount. Return a single total.

## Worked solution and explanation

### Why this problem exists in real interviews

This challenge targets grouped aggregation against `transactions`, `users`. Getting the grouping wrong on `quantity`, `total_amount`, `transaction_date` produces silently incorrect counts, which is exactly the trap interviewers set.

---

### Break down the requirements

#### Step 1: Join `transactions` with `users`

Use `JOIN` on `user_id` to link fact rows to the dimension table. INNER JOIN keeps only matched rows.

#### Step 2: Group by `username`

`GROUP BY b.username` produces one row per distinct value of the grouping column.

#### Step 3: Aggregate with SUM

`SUM(quantity)` computes the metric at the grouped grain.

#### Step 4: Order by the metric

Sort descending to surface the top values first.

---

### The solution

**Group-aggregate for active user revenue april**

```sql
SELECT
    b.username,
    SUM(quantity) AS sum_quantity
FROM transactions a
JOIN users b ON a.user_id = b.user_id
GROUP BY b.username
ORDER BY sum_quantity DESC
```

> **Cost Analysis**
>
> The main table has 150M rows (7 GB). The GROUP BY reduces the row count early, keeping downstream operations cheap. The smaller dimension table keeps the join selective. An index on the filter or join column would improve performance at scale.

> **Interviewers Watch For**
>
> Strong candidates state the correct `GROUP BY` grain before writing any SQL, showing they think about the output shape first. Join type selection (INNER vs LEFT) reveals whether the candidate read the requirements carefully.

> **Common Pitfall**
>
> Selecting a non-aggregated column without including it in `GROUP BY` is the most common error. Some engines reject it; others silently return arbitrary values.

---

## Common follow-up questions

- The `product_id` column in `transactions` has roughly 5% NULLs. How does your query handle those rows, and would the result change if NULLs were replaced with zeros? _(Tests whether the candidate understands how NULLs propagate through aggregation functions and whether their WHERE/JOIN conditions implicitly filter them out.)_
- If `transactions` and `users` have a one-to-many relationship, how does that affect the COUNT in your GROUP BY? _(Tests understanding of fan-out: joining before grouping can inflate counts.)_
- `transaction_id` in `transactions` has ~150M distinct values. What index strategy keeps your query from doing a full table scan? _(Tests whether the candidate can design indexes for high-cardinality columns and understands selectivity.)_
- Could you express this same logic as a single query without CTEs or subqueries? What readability trade-off does that introduce? _(Tests whether the candidate can flatten nested logic and understands when decomposition aids maintainability.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/active_user_revenue_for_april)
- [SQL Interview Questions](https://datadriven.io/sql-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.