# Total User Spend

> Each customer's total. Summarized.

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

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

Show each user's ID, username, and total transaction amount, alphabetically by username.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests a basic join with aggregation and ordering. Interviewers check that you can join two tables, aggregate correctly, and sort by a non-aggregated column.

---

### Break down the requirements

#### Step 1: Join users to transactions

`JOIN transactions ON users.user_id = transactions.user_id` matches each user with their purchases.

#### Step 2: Aggregate total per user

`GROUP BY users.user_id, username` with `SUM(total_amount)` computes each user's total spend.

#### Step 3: Order alphabetically

`ORDER BY username` sorts the output alphabetically by username as requested.

---

### The solution

**Join, aggregate, and sort alphabetically**

```sql
SELECT u.user_id, u.username, SUM(t.total_amount) AS total_spend
FROM users u
JOIN transactions t ON u.user_id = t.user_id
GROUP BY u.user_id, u.username
ORDER BY u.username
```

> **Cost Analysis**
>
> The join expands to 60M rows (matching transactions to users). The GROUP BY reduces to ~3M users with transactions. The alphabetical sort on 3M usernames is the secondary cost. An index on `transactions(user_id)` helps the join.

> **Interviewers Watch For**
>
> Whether you use INNER JOIN (only users with transactions) vs LEFT JOIN (all users including those with zero spend). The prompt says "total transaction amount," implying only users with transactions.

> **Common Pitfall**
>
> Sorting by `total_spend` instead of `username`. Read the prompt carefully: it asks for alphabetical order, not by spend.

---

## Common follow-up questions

- What if you needed to include users with zero transactions showing 0? _(Switch to LEFT JOIN and COALESCE(SUM(...), 0).)_
- How would you show only users with spend above a threshold? _(Tests HAVING vs WHERE for aggregate filters.)_
- What if usernames were not unique? _(Grouping by user_id alone ensures correctness; username is just for display.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/total_user_spend)
- [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.