# Total Compute Cloud Cost

> Total compute spend. The number.

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

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

The FinOps team is reconciling the compute budget line item. What is the total cloud spend for the EC2 service?

## Worked solution and explanation

### Why this problem exists in real interviews

This is a fundamental filter-and-aggregate pattern. Interviewers use it to verify that candidates can write a clean `WHERE` + `SUM` query without overcomplicating it.

---

### Break down the requirements

#### Step 1: Filter to the compute service

`WHERE svc_name = 'compute'` restricts to only rows for the target service.

#### Step 2: Sum the amount

`SUM(amount)` returns a single number representing total compute spend.

---

### The solution

**Filter and aggregate a single service**

```sql
SELECT SUM(amount) AS total_compute_cost
FROM cloud_costs
WHERE svc_name = 'compute'
```

> **Cost Analysis**
>
> With 200 distinct services, the filter reduces 8M rows to roughly 40K. An index on `svc_name` turns this into an index seek plus a sum scan over the matching rows.

> **Interviewers Watch For**
>
> Overcomplicating with GROUP BY when only a single scalar is needed. No GROUP BY is required because the entire result is one row.

> **Common Pitfall**
>
> Case sensitivity in the filter. If `svc_name` stores mixed-case values, `WHERE LOWER(svc_name) = 'compute'` would be safer, though it prevents index usage.

---

## Common follow-up questions

- What does SUM return if no rows match the filter? _(Tests knowledge that SUM returns NULL (not 0) when no rows match.)_
- How would you compute cost per region for the compute service? _(Tests adding a GROUP BY region to the existing filter.)_
- What if the table had a date column and you needed monthly totals? _(Tests date truncation combined with GROUP BY for time-series aggregation.)_

## Related

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