# Total Engineering Cost Allocation

> Engineering's total allocated budget.

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

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

The CFO is reviewing the cloud allocation total for engineering. Engineering teams in the allocation table are 'data-eng', 'backend', 'devops', and 'platform' (case-insensitive). What is the sum of allocated amounts across those teams?

## Worked solution and explanation

### What this is really asking

`team_name` is free-text, so 'data-eng', 'Data-Eng', and 'DATA-ENG' all live in those 8M rows. One `SUM(amount)` filtered by a case-insensitive membership check is the whole query.

---

### Break down the requirements

#### Step 1: Normalize the team name

Wrap `team_name` in `LOWER()` so casing variants collapse to one bucket before the IN check fires.

#### Step 2: Restrict to the four engineering teams

`IN ('data-eng', 'backend', 'devops', 'platform')`. The CFO did not ask for anything outside that set.

#### Step 3: Sum the amount

Single scalar output. No GROUP BY, no per-team breakdown unless asked.

---

### The solution

**TOTAL ENGINEERING SPEND**

```sql
SELECT SUM(amount) AS total_amount
FROM cost_allocs
WHERE LOWER(team_name) IN ('data-eng', 'backend', 'devops', 'platform');
```

> **Cost Analysis**
>
> Full scan over 8M rows. `LOWER()` defeats any index on `team_name`. A functional index on `LOWER(team_name)` or a normalized column at insert pays for itself if this runs often.

> **Interviewers Watch For**
>
> Whether you ask about casing without prompting. Free-text team labels almost always carry mixed case. Naming the assumption out loud is worth more than the fix.

> **Common Pitfall**
>
> `SUM` over an empty result returns NULL, not zero. Wrap it in `COALESCE(SUM(amount), 0)` if the CFO expects a number on a quiet quarter.

> **The False Start**
>
> First instinct is `WHERE team_name IN ('data-eng', 'backend', 'devops', 'platform')`. Symptom: rows stored as `'Data-Eng'` or `'DATA-ENG'` fall out of the sum. Pivot to `LOWER(team_name) IN (...)`.

---

### COMMON FOLLOW-UP QUESTIONS

## Common follow-up questions

- How would you produce a per-team breakdown instead of one total? _(Swap to `GROUP BY LOWER(team_name)` and select the normalized name.)_
- What if the CFO asks for only the current quarter? _(Add a `period` predicate aligned with the partition key so the planner prunes partitions.)_
- How do you guard against future team-name typos? _(Promote the four teams into a dim table with stable `team_id`, then filter on the surrogate key.)_

## Related

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