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

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 12 rows
metric_iddepartmentmetric_namemetric_valuefiscal_quarterfiscal_year
1740Engineeringheadcount92Q12025
10004240Engineeringheadcount92Q12025
510Engineeringheadcount73Q32025
10004210Engineeringheadcount73Q32025
4200Engineeringheadcount30Q12025

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' AND metric_value <= 30 OR metric_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 OR metric_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.

Scoped OR with correct precedence
SELECT *
FROM employee_metrics
WHERE metric_name = 'headcount'
  AND (metric_value <= 30 OR metric_value >= 60)
ORDER BY metric_value DESC, metric_id

Common follow-up questions