Top of the Bill

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
Asked in
Slickdeals

Problem

Finance is reviewing the cloud bills and wants to see which services dominate the spend. Among the services that also appear in the cost allocations, list the two with the highest total cloud cost, the biggest spender first.

Every cloud bill has a headliner or two. Find them.

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 2 of 2 rows
svc_nametotal_amount
BigQuery17503.4
CloudFront17296.920000000002

Worked solution and explanation

What this really is

Underneath the FinOps framing this is a semi-join feeding a top-N. The real question the interviewer is probing: can you express 'services that also have allocations on file' as a membership test, not a row-multiplying join? Reach for a plain INNER JOIN on svc_name and every service with many allocation rows gets its cloud cost summed once per allocation, so the totals balloon and the top two you report is not the top two finance sees. cost_allocs is a filter here, never a source of dollars: the moment you SUM across the join, or add its amounts in, the numbers stop tying out.


Reading the requirements

Step 1: Restrict to allocated services without fanning out

The requirement is membership, not a join: cc.svc_name IN (SELECT svc_name FROM cost_allocs). This keeps each cloud_costs row exactly once. An INNER JOIN would duplicate a cost row once per matching allocation and inflate SUM(cc.amount).

Step 2: Aggregate and take the two heaviest

SUM(cc.amount) GROUP BY cc.svc_name gives per-service spend, then ORDER BY total_amount DESC LIMIT 2 takes the two heaviest. Sum cc.amount only; cost_allocs is a filter, never a metric source.


The solution

TOP TWO SERVICES BY COST
SELECT cc.svc_name,
       SUM(cc.amount) AS total_amount
FROM cloud_costs cc
WHERE cc.svc_name IN (SELECT svc_name FROM cost_allocs)
GROUP BY cc.svc_name
ORDER BY total_amount DESC
LIMIT 2;
Wrong (join fans out)
INNER JOIN cost_allocs ca ON cc.svc_name = ca.svc_name duplicates each cost row once per matching allocation, so SUM(cc.amount) is multiplied by the allocation count per service.
Right (semi-join)
cc.svc_name IN (SELECT svc_name FROM cost_allocs) is a membership test: each cost row survives exactly once and the sum stays honest.

Common follow-up questions

Common follow-up questions