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

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

employee_metrics

Columns: metric_id INTEGER, department TEXT, metric_value REAL, metric_name TEXT, 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 200 rows
emp_nametotal_metric_value
Alice Chen412
Bob Martinez0
Carol Wu528.8
David Kim0
Eve Johnson277.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

LEFT JOIN with the Q1 filter in ON, zero-filled
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_name

Common follow-up questions