The Quiet Drain

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

Problem

How many different AWS services in each region of cloud_costs have at least one high-cost line item, from the most down to the fewest? The FinOps analyst asking counts any AWS line item of 200 or more as high-cost, however the provider name happens to be capitalized.

Some AWS services quietly drain the budget.

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 6 rows
regionservice_count
us-west-22
us-central12
europe-west12
us-east-11
ap-southeast-11

Worked solution and explanation

What this is really asking

Under the FinOps framing this is a per-region count of different services, and the whole question is which unit you count. Every qualifying AWS service appears on several line items in cloud_costs (billed in the same region bill after bill), so COUNT(*) reports line items: 6 for 'us-east-1', where only CloudFront qualifies. The second trap is the dirty provider column. The vendor is stored as both 'aws' and 'AWS', and an exact match throws away every EC2 and Cloud Run row. Fold the case before filtering, and count each svc_name once per region.


The two traps hiding in the data

Step 1: Normalize the provider casing

The sample rows carry 'aws' and 'AWS' for the same vendor. WHERE provider = 'aws' keeps only the lowercase spelling, which on this data holds just BigQuery and CloudFront. LOWER(provider) = 'aws' folds both spellings into one filter before anything else runs.

Step 2: Apply the floor, then count services once

Keep entries where amount >= 200 (200 itself is high-cost). That also drops the negative credits and the rows with no amount. Group by region and use COUNT(DISTINCT svc_name) so repeat line items for the same service collapse to one.

Step 3: Sort most to fewest

ORDER BY service_count DESC is the whole order. Four regions tie at 2 and two at 1. The question only asks for most to fewest, so tied regions may come back in any order and no extra sort column is owed.


The solution

Distinct AWS services per region
SELECT region,
       COUNT(DISTINCT svc_name) AS service_count
FROM cloud_costs
WHERE LOWER(provider) = 'aws'
  AND amount >= 200
GROUP BY region
ORDER BY service_count DESC
Line-item volume
SELECT region, COUNT(*) FROM cloud_costs WHERE provider = 'aws' AND amount >= 200 GROUP BY region counts every qualifying row and misses the 'AWS'-cased ones, so it answers a different question than the one asked.
Different services
COUNT(DISTINCT svc_name) with LOWER(provider) = 'aws' counts services rather than rows and captures both spellings, which is the number FinOps actually wants.

Common follow-up questions

Common follow-up questions