Where the Money Pools

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
Forbes

Problem

Each row of cloud_costs is one billing line for a service in a region, and credits appear as negative amounts. Show the total cost of each service billed in 'us-west-2', from highest spend to lowest.

Top spenders in one region.

Tables

cloud_costs

Columns: cost_id INTEGER, provider TEXT, svc_name TEXT, region TEXT, amount REAL, acct_id TEXT, bill_date TEXT

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
svc_nametotal_cost
Cosmos DB7429.28
S36573.4400000000005
Cloud Run5892.9
Lambda4609.14
Pub/Sub4383.08

Worked solution and explanation

Totals, not lines

This is a one-region rollup dressed up as a billing report: many billing lines per service have to collapse into one number per service before anything is ordered. Everyone writes the region filter. What separates candidates is whether they order totals or order lines. Sort the 34 'us-west-2' lines by amount and you return 34 rows with every service repeated. Compare services by their biggest single charge and 'Pub/Sub' (1,410.07) lands above 'Lambda' (1,303.09). Summed, 'Lambda' is at 4,609.14 and 'Pub/Sub' at 4,383.08, because 'Pub/Sub' carries two -93.70 credits that only a full sum sees.

The flat sort that looks finished
SELECT svc_name, amount
FROM cloud_costs
WHERE region = 'us-west-2'
ORDER BY amount DESC

One row per billing line: 34 rows, each service repeated.

Building it

Step 1: Keep only the 'us-west-2' lines

WHERE region = 'us-west-2' runs before any grouping, so the sum only ever sees that region's 34 lines. Drop it and all ten services from six regions come back, with totals that mean nothing for this region.

Step 2: Collapse each service to one total

GROUP BY svc_name with SUM(amount) folds every line into one row per service. The key is the service, not the vendor, so lines billed under 'GCP', 'gcp', 'Azure', 'aws' and 'AWS' all land in the same total, and the credits net out as they would on the invoice.

Step 3: Order the totals, not the lines

ORDER BY total_cost DESC sorts the five computed totals. The alias works in ORDER BY because that clause is evaluated after the SELECT list; it would not work in WHERE.

Filter, aggregate, and sort by spend
SELECT svc_name, SUM(amount) AS total_cost
FROM cloud_costs
WHERE region = 'us-west-2'
GROUP BY svc_name
ORDER BY total_cost DESC

Common follow-up questions