Allocations in Top Spending Region

A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
medium
Seniority
L3

Problem

Each row of cloud_costs is one billed line item with its region and amount, and each row of cost_allocs is a team's allocation of spend to a region. Find the region whose billed line items add up to the most, then list every allocation record for that region in full, lowest allocation id first.

The biggest spenders live 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

cost_allocs

Columns: alloc_id INTEGER, team_name TEXT, svc_name TEXT, region TEXT, amount REAL, period TEXT, acct_id TEXT, category TEXT

cost_allocs: 10 of 200 rows
alloc_idteam_namesvc_nameregionamountperiodacct_idcategory
129data-engS3us-west-283.212026-02acct-101storage
158ml-teamRDSeu-west-1156.422026-03acct-102network
187frontendLambdaap-south-1229.632026-04acct-103database
216backendBigQueryus-central1-120.52026-05acct-104ml
245devopsCloudFronteurope-west1376.052026-06acct-105other
274securityEKSap-southeast-1449.262026-07acct-106compute
303mobileSQSeu-central-1522.472026-08acct-107storage
332PlatformEC2us-east-1595.682026-09NULLnetwork
361DATA-ENGS3us-west-2668.892026-01acct-109database
390platformRDSeu-west-1742.12026-02acct-110ml

Expected output

First 5 of 26 rows
alloc_idteam_namesvc_nameregionamountperiodacct_idcategory
129data-engS3us-west-283.212026-02acct-101storage
361DATA-ENGS3us-west-2668.892026-01acct-109database
593mobileS3us-west-21254.572026-09acct-102other
3290data-engEC2us-west-210039.32023-10NULLdatabase
3522DATA-ENGEC2us-west-210768.342024-06acct-218other

Worked solution and explanation

This is an argmax over a per-region sum, dressed up as a FinOps allocation pull. Anyone can write the final filter on cost_allocs; what separates candidates is where the region comes from. 'Highest total spend' means summing every amount in cloud_costs per region and keeping the top total, not finding the single largest bill. On this data the biggest line item, 1784.5, sits in 'us-central1', while 'us-west-2' adds up to more. Grab the max row and you return every allocation for the wrong region, and nothing in the output looks broken.

Getting to the answer

Step 1: Total amount per region in cloud_costs

The ranking lives in the cost table, so GROUP BY region with SUM(amount) there. SUM skips the NULL amounts and nets the negative credits in, which is exactly what a bill total means. Leave cost_allocs alone at this point: it has its own region and amount, and its totals answer a different question.

Step 2: Keep only the top region

ORDER BY SUM(amount) DESC, region ASC with LIMIT 1 reduces the totals to one region, and the top_region CTE gives that value a name. The region ASC key never fires on this seed because no two totals match, but it is what keeps the pick deterministic the day two regions do.

Step 3: Filter cost_allocs to that region

Compare each allocation's region to (SELECT region FROM top_region), a scalar subquery that yields the one winning region. Return every allocation column and sort by alloc_id, lowest first.

Top-spend region, then its allocations
WITH top_region AS (
  SELECT region
  FROM cloud_costs
  GROUP BY region
  ORDER BY SUM(amount) DESC, region ASC
  LIMIT 1
)
SELECT
  ca.alloc_id,
  ca.team_name,
  ca.svc_name,
  ca.region,
  ca.amount,
  ca.period,
  ca.acct_id,
  ca.category
FROM cost_allocs ca
WHERE ca.region = (SELECT region FROM top_region)
ORDER BY ca.alloc_id;

top_region holds one value; the outer query filters cost_allocs on it.

Biggest line item
ORDER BY amount DESC LIMIT 1 on cloud_costs returns the region of one bill: 'us-central1'. Every allocation that follows belongs to the wrong region.
Biggest total
GROUP BY region, then ORDER BY SUM(amount) DESC LIMIT 1, returns the region whose bills add up to the most: 'us-west-2', with its 26 allocation records.

What the interviewer is really checking

Common follow-up questions