Quarterly Peak Cloud Costs

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

Domain
SQL
Difficulty
hard
Seniority
L4
Asked in
City and County of San Francisco

Problem

Report every cloud service's highest monthly cost in Q1, Q2, Q3 and Q4 as four separate columns, services listed alphabetically, where a month's cost is the total of that service's cloud_costs charges sharing one billing month. The finance team's quarterly review counts only charges tied to a billing account, so line-items that arrive with no account attached stay out of every total.

Every quarter has a peak bill.

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 10 rows
svc_nameq1_maxq2_maxq3_maxq4_max
BigQuery1572.041750.34786.022285.24
Blob Storage1999.961534.881821.661713.18
Cloud Run2142.61517.051072.8NULL
CloudFront1611.43999999999982071.27999999999971892.981035.64
Cosmos DB2392.22000000000031679.021857.32839.51

Worked solution and explanation

What this problem really is

Strip off the finance-dashboard costume and this is a two-level aggregation. The peak you are asked for is the biggest MONTH, so you have to build monthly totals first (one row per service per billing month, summing that month's charges) and only then take the MAX inside each quarter. Anyone can write a GROUP BY; the skill is refusing to collapse straight to the quarter. Take MAX(amount) per quarter and you report the biggest single charge; bucket months by their number alone and January 2024 fuses with January 2025 before you ever find the peak. Both answers look plausible and both are wrong.


Break down the requirements

Step 1: Find the real grain and the filter

cloud_costs holds 200 sample rows across 10 services, with bill_date spanning 2022-01 through 2026-09 and always falling on the first of the month. 22 line-items carry no acct_id, and SUM would fold their amounts straight into a month's total, so excluding unattributed charges has to be an explicit filter, not something the aggregate does for you.

Step 2: Roll up to monthly totals per service

Confirm the grain: many (svc_name, bill_date) pairs carry MORE THAN ONE line-item, so a service's monthly cost is the SUM of its line-items within a billing month, not a single raw row amount. This is what makes the two-level CTE necessary.

Step 3: Exclude unattributed charges, then sum

CTE monthly: every bill_date is the first of its billing month, so grouping by (svc_name, bill_date) already yields one row per service per calendar month with the year still attached. Filter acct_id IS NOT NULL so unattributed line-items stay out of every month's SUM.

Step 4: Pivot the four quarters into columns

Pull the month number out with strftime('%m', bill_date) and CAST it to an integer. The cast matters: the text '02' never compares BETWEEN 1 and 3, so without it every quarter column comes back empty. Then MAX(CASE WHEN mth BETWEEN ... END) per quarter, grouped by svc_name and ordered alphabetically.


The solution

Monthly rollup + quarterly pivot
WITH monthly AS (
    SELECT svc_name,
           CAST(strftime('%m', bill_date) AS INTEGER) AS mth,
           SUM(amount) AS monthly_cost
    FROM cloud_costs
    WHERE acct_id IS NOT NULL
    GROUP BY svc_name, bill_date
)
SELECT svc_name,
       MAX(CASE WHEN mth BETWEEN 1  AND 3  THEN monthly_cost END) AS q1_max,
       MAX(CASE WHEN mth BETWEEN 4  AND 6  THEN monthly_cost END) AS q2_max,
       MAX(CASE WHEN mth BETWEEN 7  AND 9  THEN monthly_cost END) AS q3_max,
       MAX(CASE WHEN mth BETWEEN 10 AND 12 THEN monthly_cost END) AS q4_max
FROM monthly
GROUP BY svc_name
ORDER BY svc_name ASC

Common follow-up questions