The Far Ends
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
- Asked in
- Lyft
Problem
Which headcount readings in employee_metrics sit at the extremes, 30 or below or 60 or above, rather than in the ordinary middle? Return those full rows, highest value first.
Ordinary hides in the middle. The story is at the extremes.
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 |
|---|---|---|---|---|---|
| 1740 | Engineering | headcount | 92 | Q1 | 2025 |
| 10004240 | Engineering | headcount | 92 | Q1 | 2025 |
| 510 | Engineering | headcount | 73 | Q3 | 2025 |
| 10004210 | Engineering | headcount | 73 | Q3 | 2025 |
| 4200 | Engineering | headcount | 30 | Q1 | 2025 |
Worked solution and explanation
What this really tests
This is an operator-precedence trap wearing a metrics-audit costume, with a boundary trap behind it. The ask looks like a plain filter: headcount rows at the low or high extreme. First, do you know that AND binds tighter than OR? Write the two value bounds without parentheses and the engine reads (metric_name = 'headcount' AND metric_value <= 30) OR metric_value >= 60, which returns every metric with a value of 60 or more, attrition rates and tenure numbers included. Second, the edges: collapse the two tails into NOT BETWEEN 30 AND 60 and the 30.0 headcount reading, exactly the row an extremes audit exists to catch, silently disappears.
- Missing parentheses (wrong)
- WHERE
metric_name= 'headcount' ANDmetric_value<= 30 ORmetric_value>= 60. AND resolves first, so this reads as (headcount AND value <= 30) OR (value >= 60), pulling in every metric with a value of 60 or more, headcount or not. - Parenthesized (correct)
- WHERE
metric_name= 'headcount' AND (metric_value<= 30 ORmetric_value>= 60). The two bounds form one condition that applies only to headcount rows.
Building it
Step 1: Filter to headcount
Start with metric_name = 'headcount'. Every returned row must satisfy this condition, so it has to sit outside the OR.
Step 2: Group the two extremes
The extremes are two-sided: metric_value <= 30 for the low end or metric_value >= 60 for the high end, both inclusive. Wrap them in parentheses so the OR is evaluated as one condition before it meets the AND.
Step 3: Order the outliers
Sort by metric_value descending so the largest headcounts lead, then by metric_id so equal values come back in a stable order.
SELECT *
FROM employee_metrics
WHERE metric_name = 'headcount'
AND (metric_value <= 30 OR metric_value >= 60)
ORDER BY metric_value DESC, metric_idCommon follow-up questions
- How would the result change if you dropped the parentheses? (Tests real understanding of AND/OR precedence.)
- How would you make both thresholds exclusive instead of inclusive? (Tests boundary handling: strict < versus <=.)
- What happens to rows where `metric_value` is NULL? (Tests that NULL comparisons are unknown and drop rows from both branches.)