# Highest Cost Per Team

> Peak cost, team by team.

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

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

The FinOps team flags any team whose peak single cost allocation looks unusually high. Show the highest allocation amount for each team so they can identify outliers.

## Worked solution and explanation

### Why this problem exists in real interviews

Interviewers use the `cost_allocs` table here to probe row numbering within partitions combined with nested subqueries. The columns `team_name`, `svc_name`, `region` force candidates to reason about the correct grain before writing any aggregation.

---

### Break down the requirements

#### Step 1: Partition by `team_name`

`PARTITION BY team_name` creates groups. Within each group, `ORDER BY alloc_id DESC` determines the ranking.

#### Step 2: Filter to rank 1

`WHERE rnk = 1` in the outer query selects the target row per group.

---

### The solution

**Row-number for highest cost team**

```sql
SELECT *
FROM (
    SELECT *,
           ROW_NUMBER() OVER (PARTITION BY team_name ORDER BY alloc_id DESC) AS rnk
    FROM cost_allocs
) ranked
WHERE rnk = 1
ORDER BY team_name
```

> **Cost Analysis**
>
> Window function sorts within each `team_name` partition. An index on `(team_name, alloc_id)` avoids a full sort.

> **Interviewers Watch For**
>
> The interviewer checks whether you use ROW_NUMBER (one row) vs. RANK/DENSE_RANK (ties) based on the prompt requirements.

> **Common Pitfall**
>
> Using GROUP BY with MIN(alloc_id) gives the value but not the other columns. ROW_NUMBER gives the full row.

---

## Common follow-up questions

- What happens to your results if `team_name` in `cost_allocs` contains trailing whitespace or mixed casing? _(Tests awareness of text normalization issues that silently fragment GROUP BY results.)_
- Your window function uses a default frame. What is the implicit frame, and would switching to ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW change anything? _(Tests knowledge of default window frames (RANGE vs ROWS) and when the distinction matters.)_
- `alloc_id` in `cost_allocs` has ~8M distinct values. What index strategy keeps your query from doing a full table scan? _(Tests whether the candidate can design indexes for high-cardinality columns and understands selectivity.)_
- If the business definition of `svc_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/highest_cost_per_team)
- [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.