# Metric Trend by Department

> How each team's numbers moved, year over year.

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

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

The VP of Engineering wants a quick read on how each department's tracked metric has moved over time. For each department and fiscal_year, compute the average metric value. Return department, fiscal_year, and the average, ordered by department then year.

## 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 employee_metrics by department and fiscal_year.

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

Compute AVG(metric_value) within each (department, fiscal_year) group.

#### Step 3: Order by the metric

Order by department then fiscal_year for a stable year-over-year view.

---

### The solution

**Group-aggregate for average headcount department**

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

> **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/metric_trend_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). DataDriven is the data engineering interview community. Live code execution in SQL, Python, and Spark sandboxes. Every feature is open to every member.