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

cloud_costs: 10 of 200 rows
cost_idprovidersvc_nameregionamountacct_idbill_date
543GCPS3us-west-219.33acct-10012026-02-01
586AzureRDSeu-west-137.16acct-10022026-03-01
629awsLambdaap-southeast-154.99acct-10032026-04-01
672gcpBigQueryus-central172.82acct-10042026-05-01
715AWSCloud Runeurope-west190.65acct-10052026-06-01
758GCPBlob Storageus-east-1108.48acct-10062026-07-01
801AzureCosmos DBus-west-2126.31acct-10072026-08-01
844awsCloudFronteu-west-1144.14acct-10082026-09-01
887gcpPub/Subap-southeast-1161.97NULL2026-01-01
930AWSEC2us-central1179.8acct-10102026-02-01

Expected output

First 5 of 5 rows
fiscal_yeartotal_spendservice_count
202215743.7200000000012
202316482.792
202417221.862
202517960.932
202689733.710

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.

Annual spend and unique services
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.

The tempting version
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