Department Spend Gap

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
L3
Asked in
Chewy

Problem

What is the absolute difference between the largest single transaction made by anyone in Engineering and the largest single transaction made by anyone in Marketing?

Gap between Engineering's and Marketing's biggest single purchase

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

employees

Columns: employee_id INTEGER, emp_name TEXT, department TEXT, salary REAL, manager_id INTEGER, hire_date TEXT, user_id INTEGER

employees: 10 of 200 rows
employee_idemp_namedepartmentsalarymanager_idhire_dateuser_id
1Alice ChenEngineering55731NULL2021-02-02100
2Bob MartinezProduct61462NULL2022-03-03197
3Carol WuDesign67193NULL2023-04-04294
4David KimMarketing72924NULL2024-05-05391
5Eve JohnsonSales78655NULL2025-06-06488
6Frank PatelHR84386NULL2026-07-07585
7Grace LeeFinance55117NULL2020-08-08682
8Hank BrownOperations60848NULL2021-09-09779
9Iris DavisData66579NULL2022-10-10876
10Jake WilsonLegal72310NULL2023-11-11973

Expected output

First 1 of 1 rows
spend_gap
40.41000000000008

Worked solution and explanation

This is two filtered maxima pulled from a table that cannot filter itself, dressed up as a spend comparison. transactions holds every amount and no department, while employees holds the department and no amount, so nothing works until you bridge them on user_id. Everyone gets the join. What separates candidates is one word in the question: absolute. On this data Engineering's biggest purchase is 1222.29 and Marketing's is 1262.70. Subtracting Marketing from Engineering gives -40.41, so a query that drops ABS is graded wrong even though every number inside it is right.

Walking to the answer

Step 1: Bridge amounts to departments on user_id

employees.user_id is the key that matches transactions.user_id. employee_id looks tempting but lands on no purchase from either department. The inner join also drops staff whose user_id is NULL, which is exactly right: no account, no purchases.

Step 2: Take one MAX(total_amount) per department

Each scalar subquery filters the joined rows to one department with WHERE e.department = ... and keeps the single biggest total_amount. The question says single transaction, so you want the MAX of a row, not the SUM of a person's spend. Every user_id sits on two employees rows. That would double a sum, but it leaves a maximum untouched.

Step 3: Subtract, then strip the sign

ABS(eng_max - mkt_max) is the gap whichever side is bigger. Intuition cannot tell you the subtraction order here, because the two maxima sit only 40.41 apart on amounts around 1,200.

The solution

Cross-department max comparison
SELECT ABS(
    (SELECT MAX(t.total_amount)
     FROM transactions t
     JOIN employees e ON t.user_id = e.user_id
     WHERE e.department = 'Engineering')
    -
    (SELECT MAX(t.total_amount)
     FROM transactions t
     JOIN employees e ON t.user_id = e.user_id
     WHERE e.department = 'Marketing')
) AS spend_gap
Signed difference
Engineering's max minus Marketing's max returns -40.41 on this data. The join is right and both maxima are right, but the sign makes the answer wrong.
Absolute gap
Wrapping the subtraction in ABS returns 40.41 whichever department is ahead, which is what 'the difference between' means to whoever asked.

Common follow-up questions