# Extreme Headcount Departments

> The pay extremes tell a story.

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

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

Pull all entries from the workforce metrics table where the metric is headcount and the value is either 10 or below, or 80 or above. Return all available fields for each matching row.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests GROUP BY with HAVING for threshold-based filtering. The interviewer checks whether you use HAVING (not WHERE) for aggregate conditions.

---

### Break down the requirements

#### Step 1: Group by `metric_name`

`GROUP BY metric_name` creates one row per group.

#### Step 2: Filter with HAVING

`HAVING COUNT(*) > 10` keeps only groups exceeding the threshold.

---

### The solution

**GROUP BY with HAVING threshold**

```sql
SELECT metric_name, COUNT(*) AS cnt
FROM employee_metrics
GROUP BY metric_name
HAVING COUNT(*) > 10
ORDER BY cnt DESC
```

> **Cost Analysis**
>
> Hash aggregation over `employee_metrics`. Low cardinality on `metric_name` makes this efficient.

> **Interviewers Watch For**
>
> The interviewer checks that you use HAVING (post-aggregation) rather than WHERE (pre-aggregation) for the count condition.

> **Common Pitfall**
>
> `WHERE COUNT(*) > N` is a syntax error. Aggregate conditions must use HAVING.

---

## Common follow-up questions

- What is the difference between WHERE and HAVING? _(Tests understanding: WHERE filters rows, HAVING filters groups.)_
- How would you also show the count per group? _(Tests adding COUNT(*) to the SELECT.)_
- How would you find the top 5 groups instead of using a threshold? _(Tests ORDER BY ... DESC LIMIT 5.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/extreme_headcount_departments)
- [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). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.