Annual Cloud Spend Summary
An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.
- Domain
- SQL
- Difficulty
- easy
- Seniority
- L3
- Asked in
- Uber
Problem
The annual infrastructure review needs a slide showing how cloud spend and service sprawl have grown, walking the timeline from the earliest fiscal year up to the most recent. For each fiscal year, report the total amount spent and how many unique services appeared on the bill.
A year of cloud bills. Add it all up.
Tables
cloud_costs
Columns: cost_id INTEGER, provider TEXT, svc_name TEXT, region TEXT, amount REAL, acct_id TEXT, bill_date DATE
| cost_id | provider | svc_name | region | amount | acct_id | bill_date |
|---|---|---|---|---|---|---|
| 543 | GCP | S3 | us-west-2 | 19.33 | acct-1001 | 2026-02-01 |
| 586 | Azure | RDS | eu-west-1 | 37.16 | acct-1002 | 2026-03-01 |
| 629 | aws | Lambda | ap-southeast-1 | 54.99 | acct-1003 | 2026-04-01 |
| 672 | gcp | BigQuery | us-central1 | 72.82 | acct-1004 | 2026-05-01 |
| 715 | AWS | Cloud Run | europe-west1 | 90.65 | acct-1005 | 2026-06-01 |
| 758 | GCP | Blob Storage | us-east-1 | 108.48 | acct-1006 | 2026-07-01 |
| 801 | Azure | Cosmos DB | us-west-2 | 126.31 | acct-1007 | 2026-08-01 |
| 844 | aws | CloudFront | eu-west-1 | 144.14 | acct-1008 | 2026-09-01 |
| 887 | gcp | Pub/Sub | ap-southeast-1 | 161.97 | NULL | 2026-01-01 |
| 930 | AWS | EC2 | us-central1 | 179.8 | acct-1010 | 2026-02-01 |
Expected output
| fiscal_year | total_spend | service_count |
|---|---|---|
| 2022 | 15743.720000000001 | 2 |
| 2023 | 16482.79 | 2 |
| 2024 | 17221.86 | 2 |
| 2025 | 17960.93 | 2 |
| 2026 | 89733.7 | 10 |
Worked solution and explanation
Two aggregates, one grain
This is a finance slide wrapped around one GROUP BY that carries two aggregates of different natures. SUM(amount) is additive: every bill line adds to it. The service count is a set cardinality: a service billed 50 times in a year still counts once. Anyone writes the sum. The trap is COUNT(svc_name) for the second column, which tallies bill lines. On this data that reports 20 for each earlier year and 120 for the current one, where the answer is 2, 2, 2, 2 and 10, so the slide overstates service sprawl about tenfold.
Step 1: Derive the year from bill_date
strftime('%Y', bill_date) turns each bill date into a 4-character year string, aliased fiscal_year. Fiscal years match calendar years here, so no month shift is needed. The mixed casing in provider ('aws' and 'AWS') never enters the key, so it can be ignored.
Step 2: Compute both metrics in one pass
SUM(amount) becomes total_spend and COUNT(DISTINCT svc_name) becomes service_count. Lines with no acct_id stay in, because the slide is about all spend. The 12 lines with a NULL amount add nothing to the sum, and the negative credit lines net against it as finance expects.
Step 3: Sort by fiscal_year
The slide walks the timeline earliest first, so ORDER BY fiscal_year ascending. Each year is a single row, so no tie can arise and the order needs no second key.
SELECT strftime('%Y', bill_date) AS fiscal_year,
SUM(amount) AS total_spend,
COUNT(DISTINCT svc_name) AS service_count
FROM cloud_costs
GROUP BY strftime('%Y', bill_date)
ORDER BY fiscal_year;One pass over cloud_costs, one row per year.
SELECT strftime('%Y', bill_date) AS fiscal_year,
SUM(amount) AS total_spend,
COUNT(svc_name) AS service_count
FROM cloud_costs
GROUP BY strftime('%Y', bill_date)
ORDER BY fiscal_year;total_spend is right and service_count reads 20 or 120. Every row mismatches.
Common follow-up questions
- What changes if `svc_name` can be `NULL` for some bill lines? (`COUNT(DISTINCT ...)` skips NULLs, so a NULL service would silently vanish from the count.)
- How would you report unique services per provider per year, given that `provider` holds both 'aws' and 'AWS'? (Tests normalizing the dirty `provider` values with `LOWER` before adding a second key.)
- When would an approximate unique count be acceptable for `service_count`? (Tests the exactness versus cost trade-off at much higher cardinality.)
- If the fiscal year started in July, how would the year key change? (Tests shifting `bill_date` by an interval before extracting the year.)