User Spend Audit

A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
medium
Seniority
L4
Asked in
Visa

Problem

Given the username 'alice', what's their total spend on Electronics products with a quality rating above 3?

One user. One category. Total spend.

Tables

products

Columns: product_id INTEGER, category TEXT, rating REAL, product_name TEXT, price REAL, in_stock INTEGER

products: 10 of 200 rows
product_idproduct_namecategorypriceratingin_stock
1001Basic Device 1XBooks17.522.71
1050Deluxe Bundle 2XClothing25.054.41
1099Pro Unit 3XHome & Kitchen32.582.11
1148Ultra Tool 4XCollectibles40.113.81
1197Essential Set 5XToys47.641.51
1246Classic System 6XBeauty03.21
1295Smart Gadget 7XAutomotive62.74.90
1344Eco Kit 8XGarden70.232.61
1393Turbo Pack 9XMusic77.764.30
1442Premium Widget 10XElectronics85.2921

transactions

Columns: transaction_id INTEGER, user_id INTEGER, product_id INTEGER, total_amount REAL, quantity INTEGER, 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_spend
828.18

Worked solution and explanation

Why this problem exists in real interviews

This tests a three-table join with multiple filter conditions. Interviewers check whether you can chain joins, apply filters at the right level, and compute an aggregate across the filtered result.


Break down the requirements

Step 1: Join users to transactions to products

users -> transactions -> products chain: match the user by username, their transactions, and the products in each transaction.

Step 2: Filter by username, category, and rating

WHERE u.username = 'alice' AND p.category = 'Electronics' AND p.rating > 3 applies all three conditions.

Step 3: Sum total spend

SUM(t.total_amount) computes the filtered total spend.


The solution

Three-table join with multi-filter aggregation
SELECT SUM(t.total_amount) AS total_spend
FROM users u
JOIN transactions t ON u.user_id = t.user_id
JOIN products p ON t.product_id = p.product_id
WHERE u.username = 'alice'
  AND p.category = 'Electronics'
  AND p.rating > 3

Common follow-up questions