# Average Headcount by Department

> Compensation benchmarks, department by department.

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

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

The VP of Engineering asked HR for a quick staffing snapshot before the reorg meeting. From the employee metrics, isolate the headcount records and compute the average headcount value per department.

## Worked solution and explanation

### Why this problem exists in real interviews

Interviewers use the `employee_metrics` table here to probe grouped aggregation. The columns `department`, `metric_name`, `metric_value` force candidates to reason about the correct grain before writing any aggregation.

---

### Break down the requirements

#### Step 1: Group by `department`

`GROUP BY` at the correct grain produces one row per group.

#### Step 2: Compute `AVG(metric_value)`

The AVG function computes the avg per group.

#### Step 3: Order by the metric

Sort by `avg_metric_value` desc for readability.

---

### The solution

**Group-aggregate for average headcount department**

```sql
SELECT
    department,
    AVG(metric_value) AS avg_metric_value
FROM employee_metrics
GROUP BY department
ORDER BY avg_metric_value DESC
```

> **Cost Analysis**
>
> The main table has 10K rows. The GROUP BY reduces the row count early, keeping downstream operations cheap.

> **Interviewers Watch For**
>
> Strong candidates state the correct `GROUP BY` grain before writing any SQL, showing they think about the output shape first.

> **Common Pitfall**
>
> Selecting a non-aggregated column without including it in `GROUP BY` is the most common error. Some engines reject it; others silently return arbitrary values.

---

## Common follow-up questions

- What happens to your results if `department` in `employee_metrics` contains trailing whitespace or mixed casing? _(Tests awareness of text normalization issues that silently fragment GROUP BY results.)_
- Your GROUP BY aggregates `metric_id` from `employee_metrics`. If two groups have the same aggregate value, how is the output ordered, and is that deterministic? _(Tests awareness that ORDER BY on a non-unique value produces non-deterministic row order without a tiebreaker.)_
- The `department` column in `employee_metrics` has a zipf distribution, meaning a few values dominate. How does that skew affect your query plan and parallelism? _(Tests understanding of data skew: the optimizer may choose a bad plan when histogram statistics are stale.)_
- If the business definition of `metric_name` changed mid-quarter (e.g., a status value was renamed), how would you handle historical consistency? _(Tests awareness of slowly changing dimensions and backward-compatible query design.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/average_headcount_by_department)
- [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.