Cloud Cost Trend Analysis

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
L4
Asked in
Tesla

Problem

FinOps wants to spot bumpy cloud bills service by service. For every individual bill, list the service, the billing date, the amount, and how much that bill moved versus the service's previous bill. The first bill on record for a service has nothing to compare against.

Cost trends across billing periods.

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 200 rows
svc_namebill_dateamountprice_change
BigQuery2025-02-011320.921069.8000000000002
Blob Storage2022-02-01465.08NULL
Cloud Run2026-01-01NULLNULL
CloudFront2024-02-01-137.4NULL
Cosmos DB2023-01-01661.21NULL

Worked solution and explanation

Why this problem exists in real interviews

This is the LAG window function in its purest form: same row count in, one extra column out, the comparison anchored per partition. The interviewer is checking that you partition by svc_name, order by bill_date, and remember that the first row per partition naturally returns NULL when there's no prior bill to subtract.


Break down the requirements

Step 1: Project the row-level columns

Project the four output columns directly from cloud_costs: svc_name, bill_date, amount, and the LAG expression as price_change.

Step 2: Compute the bill-over-bill delta

amount - LAG(amount) OVER (PARTITION BY svc_name ORDER BY bill_date, cost_id) gives the delta versus the previous bill of the same service. Partitioning by svc_name ensures the LAG never crosses service boundaries; adding cost_id after bill_date makes the ordering total, so two bills a service posts on the same date still have a well-defined predecessor.

Step 3: Sort the output

ORDER BY svc_name, bill_date, cost_id puts each service's bills together in date order, with cost_id settling any same-date pair so the row order is fully determined. The first row per service has no prior bill so price_change is NULL.


The solution

LAG over (PARTITION BY `svc_name` ORDER BY `bill_date`, `cost_id`)
SELECT
    svc_name,
    bill_date,
    amount,
    amount - LAG(amount) OVER (PARTITION BY svc_name ORDER BY bill_date, cost_id) AS price_change
FROM cloud_costs

Common follow-up questions