# Cloud Cost by Team

> Spend by team. Who's burning most?

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

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

The VP of Engineering wants to see which teams are driving the most cloud spend for the budget review. Show each team's total cost allocation, from highest to lowest.

## Worked solution and explanation

### Why this problem exists in real interviews

This is a fundamental GROUP BY with SUM and ORDER BY. It screens for the ability to aggregate a single metric by a dimension and sort the results.

---

### Break down the requirements

#### Step 1: Group by team

`GROUP BY team_name` produces one row per team.

#### Step 2: Sum and sort

`SUM(amount)` for total cost, `ORDER BY SUM(amount) DESC` for highest to lowest.

---

### The solution

**Simple team-level aggregation**

```sql
SELECT team_name, SUM(amount) AS total_cost
FROM cost_allocs
GROUP BY team_name
ORDER BY total_cost DESC
```

> **Cost Analysis**
>
> Single scan of 10M rows. GROUP BY reduces to a small number of teams (typically under 50). Trivially fast.

> **Common Pitfall**
>
> If `team_name` contains NULLs, they are grouped into a single NULL row. Depending on requirements, you may need `WHERE team_name IS NOT NULL`.

---

## Common follow-up questions

- What if you needed the top 5 teams only? _(Tests LIMIT 5 and whether ORDER BY is required before LIMIT.)_
- How would you show each team's percentage of total spend? _(Tests window function: 100.0 * SUM(amount) / SUM(SUM(amount)) OVER ().)_
- What if amount included negative values for credits? _(SUM handles negatives correctly, but the interpretation changes; tests business logic awareness.)_

## Related

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