# Annual Cloud Spend

> One year of cloud bills. The total.

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

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

Budget planning kicks off next week and the FinOps lead asked for a year-over-year view of total cloud spend. Sum all costs for each billing year and present the results chronologically.

## Worked solution and explanation

### Why this problem exists in real interviews

Built around `cloud_costs`, this problem requires date extraction and truncation. Interviewers watch whether candidates handle the relationship between `provider`, `svc_name`, `region` correctly under grouping pressure.

---

### Break down the requirements

#### Step 1: Group by `STRFTIME('%Y'`

`GROUP BY` at the correct grain produces one row per group.

#### Step 2: Compute `SUM(amount)`

The SUM function computes the sum per group.

#### Step 3: Order by the metric

Sort by `sum_amount` desc for readability.

---

### The solution

**Date-extract for annual cloud spend**

```sql
SELECT
    STRFTIME('%Y', bill_date) AS year, STRFTIME('%Y', bill_date), provider,
    SUM(amount) AS sum_amount
FROM cloud_costs
GROUP BY STRFTIME('%Y', bill_date), provider
ORDER BY sum_amount DESC
```

> **Cost Analysis**
>
> The main table has 5M rows (1 GB). Partitioned on `bill_date`, so queries filtering on that column skip most partitions. 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 `provider` in `cloud_costs` contains trailing whitespace or mixed casing? _(Tests awareness of text normalization issues that silently fragment GROUP BY results.)_
- Your GROUP BY aggregates `cost_id` from `cloud_costs`. 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.)_
- `cost_id` in `cloud_costs` has ~5M 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 `cost_id` in `cloud_costs` contained negative values, would your query still produce correct results? _(Tests whether the candidate validated assumptions about the domain of numeric columns.)_

## Related

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