Metric Value Quarter Complement
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
- L4
- Asked in
- Airbnb
Problem
List every entry in employee_metrics where its metric value, rounded to the nearest whole number, plus the number in its fiscal quarter label (Q1 through Q4) comes to exactly 5. The budget allocation formula under test reads those entries in entry id order, lowest first.
2 metrics that accidentally match.
Tables
employee_metrics
Columns: metric_id INTEGER, department TEXT, metric_name TEXT, metric_value REAL, 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
| metric_id | department | metric_name | metric_value | fiscal_quarter | fiscal_year |
|---|---|---|---|---|---|
| 674 | Sales | training_hours | 2.2 | Q3 | 2026 |
| 1248 | Operations | diversity_ratio | 4.4 | Q1 | 2025 |
| 10004214 | Sales | training_hours | 2.2 | Q3 | 2026 |
| 10004228 | Operations | diversity_ratio | 4.4 | Q1 | 2025 |
Worked solution and explanation
This is 2 type conversions dressed up as a budget formula. metric_value is a float and fiscal_quarter is a label like 'Q3', and the formula only means something once both are whole numbers. Everyone spots the 'Q'. What separates candidates is honouring 'nearest whole number' and knowing which conversion actually rounds. Drop the fraction instead, with FLOOR or a truncating habit carried over from SQLite, and 1.5 stays 1 and 3.7 stays 3: entries 2355 and 2929 and their 2 cloned copies slip in, and the answer swells from 4 rows to 8.
Getting both sides to whole numbers
Step 1: Pull the digit out of the quarter label
SUBSTR(fiscal_quarter, 2) gives you the text '3'. Compared against a number, the editor's SQL reads it as 3. A strict engine refuses that comparison, so on one of those you would cast the digit as well.
Step 2: Round with CAST(metric_value AS INTEGER)
Converting a float to an integer rounds to the nearest whole number in the SQL this editor speaks, as it does in PostgreSQL and Trino: 2.2 becomes 2, 1.5 becomes 2 and 3.7 becomes 4. ROUND(metric_value) lands on the same whole numbers here; the cast also hands back an integer type for the formula to compare.
Step 3: Compare the quarter to what is left of 5
Saying the quarter number equals 5 - CAST(metric_value AS INTEGER) is the same formula as 'the sum is 5', with each side one whole number. Rows with a missing metric_value drop out on their own: the right side is NULL, and NULL never equals anything.
Step 4: Sort by metric_id
The ids are not sequential (cloned entries sit above 10,000,000), so you cannot assume any order; the sort is what produces it.
SELECT
metric_id,
department,
metric_name,
metric_value,
fiscal_quarter,
fiscal_year
FROM employee_metrics
WHERE SUBSTR(fiscal_quarter, 2) = 5 - CAST(metric_value AS INTEGER)
ORDER BY metric_id- Dropping the fraction
FLOOR(metric_value)keeps 1.5 as 1 and 3.7 as 3. Paired with Q4 and Q2, both rows land on exactly 5, and 2355 and 2929 join an answer they do not belong to.- Rounding
CAST(metric_value AS INTEGER)turns 1.5 into 2 and 3.7 into 4. Paired with Q4 and Q2, both rows come out to 6 and stay out, leaving 674 and 1248 and their clones.
Common follow-up questions
- What changes if `fiscal_quarter` starts arriving as 'FY26-Q3'? (Tests parsing by position versus by pattern.)
- What if `metric_value` can be negative, say -1.5, or sit on a half like 2.5? (Engines disagree on halves (away from zero versus to even): which rule does the formula want, and how do you pin it?)
- How would you model the quarter so this filter could use an index? (Tests storing the number as a column instead of re-deriving it in every query.)