Department Spend Difference

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

Leadership wants to compare peak metric values between engineering and marketing. Find the highest metric value recorded in each of the two departments, then return the absolute gap between them as a single number.

The compensation gap between departments.

Tables

employee_metrics

Columns: metric_id INTEGER, department TEXT, metric_name TEXT, metric_value REAL, fiscal_quarter TEXT, fiscal_year INTEGER

employee_metrics: 10 of 200 rows
metric_iddepartmentmetric_namemetric_valuefiscal_quarterfiscal_year
141Productattrition_rate7.3Q22025
182Designavg_tenure_months14.6Q32026
223Marketingsatisfaction_score21.9Q42024
264Salestraining_hours29.2Q12025
305HRpromotions36.5Q22026
346Financeopen_positions43.8Q32024
387Legaltime_to_hire_days51.1Q42025
428Operationsdiversity_ratio58.4Q12026
469Dataremote_percentage65.7Q22024
510Engineeringheadcount73Q32025

Expected output

First 1 of 1 rows
absolute_difference
2.9000000000000057

Worked solution and explanation

Strip the costume off this one and it is a two-scalar subtraction wrapped in an absolute value. The business asks for a 'gap between departments,' but the real skill is computing two independent aggregates and folding them into one result row without assuming which side is bigger. Everyone reaches for MAX. What separates a clean answer is keeping the two department filters isolated, resisting GROUP BY when the output is a single number, and refusing the bare subtraction: on this data Marketing's peak beats Engineering's, so Engineering minus Marketing comes back negative.

The trap: GROUP BY when the answer is one row

The instinct on anything that says 'each department' is GROUP BY department. Do that here and you get two rows back, one per department, and then you are stuck trying to subtract one row from another, which SQL does not do the way you want. The output is a single scalar: the absolute difference. The moment you see one number in the expected preview, that is your signal to compute each side as its own scalar subquery, not as grouped rows.

Step 1: Get the peak for Engineering as a scalar

A subquery that filters to department = 'Engineering' and applies MAX(metric_value) returns exactly one value. Because it produces a single row and a single column, SQL treats it as a scalar you can drop into an arithmetic expression. No grouping needed: the WHERE already pins you to one department.

Step 2: Do the same for Marketing

Write an identical scalar subquery with department = 'Marketing'. Keeping the two filters in separate subqueries is what guarantees the departments never bleed into each other. This is the whole game: two clean, independent peaks.

Step 3: Subtract and wrap in ABS

Subtract the two scalars and wrap the result in ABS(). On this seed Engineering peaks at 92.0 and Marketing at 94.9, so a bare Engineering minus Marketing returns -2.9. ABS() folds the sign away, so you never have to reason about which department is larger. That is why the prompt says 'absolute gap': it frees you from a CASE expression or a conditional swap.

Absolute peak-metric gap
SELECT ABS(
  (SELECT MAX(metric_value) FROM employee_metrics WHERE department = 'Engineering')
  - (SELECT MAX(metric_value) FROM employee_metrics WHERE department = 'Marketing')
) AS absolute_difference

Two scalar subqueries, one subtraction, one ABS. No GROUP BY, no join.

Overbuilt (self-join)
Put GROUP BY department into a CTE, then self-join the Engineering row to the Marketing row on a constant to subtract. It works, but it is three times the code and invites a join-condition mistake.
Clean (scalar subqueries)
Two scalar subqueries subtracted inside ABS. Each subquery scans for one department's MAX independently. It reads top to bottom, with no join reasoning required.

Common follow-up questions