Three-Value Sum Combinations

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
Lyft

Problem

Find all combinations of 3 different employee metric records whose values sum to exactly 25. Each combination must use three separate records with no reuse. Show the three values in each qualifying combination.

Pick three. See what they add up to.

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 5 of 35 rows
val1val2val3
1.58.115.4
1.58.115.4
1.515.48.1
1.58.115.4
8.88.18.1

Worked solution and explanation

The mental model: k-combinations as a self-join

This is identity-based combination counting dressed up as a metrics puzzle. Anyone can chain three copies of employee_metrics; what separates candidates is which column keeps the copies apart. Chain them on metric_id and every group of three records comes back exactly once, equal values included. Order or dedupe on metric_value instead and you quietly drop triples like 8.8 + 8.1 + 8.1, where two different records share a value, or collapse the record triples that repeat the same numbers. Either way the row count no longer matches.


The three traps

Step 1: No-reuse is not the same as no-duplicate-output

Beginners reach for e1.metric_id != e2.metric_id AND e2.metric_id != e3.metric_id AND e1.metric_id != e3.metric_id. That correctly enforces three separate records, but each underlying combination still surfaces six times, once per permutation (1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), (3,2,1). The strict inequality chain e1.metric_id < e2.metric_id AND e2.metric_id < e3.metric_id collapses all six permutations into the single canonical ascending ordering, so dedup is built into the join itself rather than bolted on with DISTINCT.

Step 2: Cartesian product cost explosion

Three copies of a 10,000-row table is 10^12 = 1 trillion tuples before any filter. The ascending-id chain keeps one ordering of each triple, roughly 167 billion, but an inequality cannot drive a hash join, so the engine still walks it as nested loops. On the 200-row seed that is C(200,3) = 1,313,400 triples and returns instantly; at 10,000 rows it stops being interactive. At that scale you would NOT reach for this pattern; you would precompute pair sums into an indexed table and look up the complement 25 - pair_sum instead.

Step 3: Order by a unique key, never by the value you are summing

The inequality must reference a column that is unique per row, and metric_id is unique on every record. If you wrote e1.metric_value < e2.metric_value AND e2.metric_value < e3.metric_value thinking it accomplished the same dedup, the strict < silently drops every triple where two records share a value. On this seed every value sits on two records, so 8.8 + 8.1 + 8.1 is a real answer and it vanishes. Combinatorics dedup is a property of row identity, not row payload.


The solution

Three-way self-join with canonical ordering on `metric_id`
SELECT e1.metric_value AS val1,
       e2.metric_value AS val2,
       e3.metric_value AS val3
FROM employee_metrics e1
CROSS JOIN employee_metrics e2
CROSS JOIN employee_metrics e3
WHERE e1.metric_id < e2.metric_id
  AND e2.metric_id < e3.metric_id
  AND e1.metric_value + e2.metric_value + e3.metric_value = 25

Common follow-up questions