The Full Picture
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
- L5
- Asked in
- DoubleVerify
Problem
The people analytics team is assembling a first-quarter scorecard: every employee's name beside the combined Q1 metric value on record for their department. Departments with nothing logged for Q1 should still list their people, each reading zero rather than dropping off.
Two tables know different things about the same people. Combine them.
Tables
employees
Columns: employee_id INTEGER, emp_name TEXT, department TEXT, salary REAL, manager_id INTEGER, hire_date TEXT, user_id INTEGER
| employee_id | emp_name | department | salary | manager_id | hire_date | user_id |
|---|---|---|---|---|---|---|
| 1 | Alice Chen | Engineering | 55731 | NULL | 2021-02-02 | 100 |
| 2 | Bob Martinez | Product | 61462 | NULL | 2022-03-03 | 197 |
| 3 | Carol Wu | Design | 67193 | NULL | 2023-04-04 | 294 |
| 4 | David Kim | Marketing | 72924 | NULL | 2024-05-05 | 391 |
| 5 | Eve Johnson | Sales | 78655 | NULL | 2025-06-06 | 488 |
| 6 | Frank Patel | HR | 84386 | NULL | 2026-07-07 | 585 |
| 7 | Grace Lee | Finance | 55117 | NULL | 2020-08-08 | 682 |
| 8 | Hank Brown | Operations | 60848 | NULL | 2021-09-09 | 779 |
| 9 | Iris Davis | Data | 66579 | NULL | 2022-10-10 | 876 |
| 10 | Jake Wilson | Legal | 72310 | NULL | 2023-11-11 | 973 |
employee_metrics
Columns: metric_id INTEGER, department TEXT, metric_value REAL, metric_name TEXT, fiscal_quarter TEXT, fiscal_year INTEGER
| metric_id | department | metric_name | metric_value | fiscal_quarter | fiscal_year |
|---|---|---|---|---|---|
| 141 | Product | attrition_rate | 7.3 | Q2 | 2025 |
| 182 | Design | avg_tenure_months | 14.6 | Q3 | 2026 |
| 223 | Marketing | satisfaction_score | 21.9 | Q4 | 2024 |
| 264 | Sales | training_hours | 29.2 | Q1 | 2025 |
| 305 | HR | promotions | 36.5 | Q2 | 2026 |
| 346 | Finance | open_positions | 43.8 | Q3 | 2024 |
| 387 | Legal | time_to_hire_days | 51.1 | Q4 | 2025 |
| 428 | Operations | diversity_ratio | 58.4 | Q1 | 2026 |
| 469 | Data | remote_percentage | 65.7 | Q2 | 2024 |
| 510 | Engineering | headcount | 73 | Q3 | 2025 |
Expected output
| emp_name | total_metric_value |
|---|---|
| Alice Chen | 412 |
| Bob Martinez | 0 |
| Carol Wu | 528.8 |
| David Kim | 0 |
| Eve Johnson | 277.6 |
Worked solution and explanation
What this problem is really about
This is an outer join dressed as a quarterly report. The real question: can you keep every employee on the sheet when only half the departments logged a Q1 metric? Anyone can join employees to employee_metrics on department and SUM the readings. The tell is where the m.fiscal_quarter = 'Q1' filter goes. Put it in a WHERE and the outer join collapses to an inner one: the 100 people in departments that never reported for Q1 silently fall off the scorecard.
Building the query
Step 1: Keep everyone with a filtered LEFT JOIN
LEFT JOIN employee_metrics m ON e.department = m.department AND m.fiscal_quarter = 'Q1'. The Q1 test rides inside the ON, so departments with no Q1 row still emit their employees, matched to nothing.
Step 2: Zero-fill with COALESCE
COALESCE(SUM(m.metric_value), 0). For an unmatched employee, SUM over zero rows is NULL; COALESCE turns that into a real 0 so the person reads 'none recorded' rather than blank.
Step 3: Roll up to one row per person
GROUP BY e.employee_id, e.emp_name. A department can log several Q1 metrics, so each employee fans out to multiple rows; the GROUP BY plus SUM collapses them into one total. Grouping on employee_id also keeps two people who share a name distinct.
The solution
SELECT e.emp_name, COALESCE(SUM(m.metric_value), 0) AS total_metric_value
FROM employees e
LEFT JOIN employee_metrics m ON e.department = m.department AND m.fiscal_quarter = 'Q1'
GROUP BY e.employee_id, e.emp_nameCommon follow-up questions
- What changes if you need Q1 of a single `fiscal_year` only? (Tests whether they add the year predicate to the ON clause, not a WHERE.)
- How would you also show how many Q1 metrics each department reported? (Tests COUNT vs SUM and NULL handling on the unmatched outer side.)
- If a department reports the same Q1 metric twice, is that double-counting or expected? (Tests awareness of grain and whether repeated readings are intended.)