# The Far Ends

> Ordinary hides in the middle. The story is at the extremes.

Canonical URL: <https://datadriven.io/problems/the_far_ends>

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

We're auditing the workforce metrics table for headcount readings at the extremes, ignoring the typical middle. Return every field for the rows whose value is 30 or below or 60 or above, highest value first.

## Worked solution and explanation

### What this really tests

This is an operator-precedence trap wearing a metrics-audit costume. The ask looks like a plain filter: headcount rows at the low or high extreme. The real question is whether you know that AND binds tighter than OR. Write the two value bounds without parentheses and the engine reads it as (metric_name = 'headcount' AND metric_value <= 30) OR metric_value >= 60, which quietly returns every metric with a value of 60 or more, not just headcount. Get that wrong and your headcount audit fills up with attrition rates and tenure numbers.

> **Parenthesize the OR**
>
> The moment a filter mixes AND with OR, wrap the OR group in parentheses. That one pair of parens is the whole problem: it scopes both value bounds under the headcount condition so they act as a single unit.

**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'. This is the condition every returned row must satisfy, 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. 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**

```sql
SELECT *
FROM employee_metrics
WHERE metric_name = 'headcount'
  AND (metric_value <= 30 OR metric_value >= 60)
ORDER BY metric_value DESC, metric_id
```

> **Interviewers watch for**
>
> The tell is the parentheses. A candidate who writes the OR bare either does not know AND/OR precedence or is not picturing how the engine parses the predicate. Both read as junior.

> **Common pitfall**
>
> Relying on left-to-right reading. SQL does not evaluate predicates in the order you typed them; AND always resolves before OR, so an unparenthesized mix silently changes the result set instead of raising an error.

> **Performance note**
>
> One sequential scan with a residual predicate. metric_name has only a handful of distinct values, so the filter cuts the row count hard before the sort, and the ORDER BY runs over the small surviving set rather than the whole table.

## Common 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.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_far_ends)
- [SQL Interview Questions](https://datadriven.io/sql-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). DataDriven is the data engineering interview community. Live code execution in SQL, Python, and Spark sandboxes. Every feature is open to every member.