Largest Single Cloud Cost
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
- Asked in
- Siemens
Problem
Which single line in cloud_costs carries the largest amount? A finance analyst chasing the biggest charge on the cloud bill needs its service name and amount, taking the service whose name comes first alphabetically when two lines tie at the top.
One line item. The biggest bill of all.
Tables
cloud_costs
Columns: cost_id INTEGER, provider TEXT, svc_name TEXT, region TEXT, amount REAL, acct_id TEXT, bill_date TEXT
| cost_id | provider | svc_name | region | amount | acct_id | bill_date |
|---|---|---|---|---|---|---|
| 543 | GCP | S3 | us-west-2 | 19.33 | acct-1001 | 2026-02-01 |
| 586 | Azure | RDS | eu-west-1 | 37.16 | acct-1002 | 2026-03-01 |
| 629 | aws | Lambda | ap-southeast-1 | 54.99 | acct-1003 | 2026-04-01 |
| 672 | gcp | BigQuery | us-central1 | 72.82 | acct-1004 | 2026-05-01 |
| 715 | AWS | Cloud Run | europe-west1 | 90.65 | acct-1005 | 2026-06-01 |
| 758 | GCP | Blob Storage | us-east-1 | 108.48 | acct-1006 | 2026-07-01 |
| 801 | Azure | Cosmos DB | us-west-2 | 126.31 | acct-1007 | 2026-08-01 |
| 844 | aws | CloudFront | eu-west-1 | 144.14 | acct-1008 | 2026-09-01 |
| 887 | gcp | Pub/Sub | ap-southeast-1 | 161.97 | NULL | 2026-01-01 |
| 930 | AWS | EC2 | us-central1 | 179.8 | acct-1010 | 2026-02-01 |
Expected output
| svc_name | amount |
|---|---|
| EC2 | 1784.5 |
Worked solution and explanation
This is an argmax dressed up as a billing audit: the question wants the line that holds the biggest amount, not the biggest amount on its own. Everyone reaches for MAX(amount), and that is the trap. MAX hands back a number with no svc_name attached, and the reflex repair, GROUP BY svc_name, quietly answers a different question: one maximum per service instead of one winner overall. Sort the real rows and keep the top one, and the service and the amount come from the same entry by construction.
Walk it to the answer
Step 1: Decide the grain: one existing row
The answer is a line already sitting in cloud_costs, not a sum and not a per-service rollup. That rules out GROUP BY: the moment you group, the result describes services, not entries.
Step 2: Sort entries by amount, biggest first
ORDER BY amount DESC puts the largest line on top. The seed also holds 12 rows with a missing amount and 10 negative credits. Credits sink to the bottom, and this grader places missing amounts after every real one in descending order, so neither can win.
Step 3: Break the tie, then keep one row
The top amount, 1784.5, appears on two lines. Both are 'EC2' on this seed, but nothing in the data promises that, so svc_name ASC decides which line wins and LIMIT 1 keeps it. Without the second key the engine may return either tied row.
SELECT svc_name, amount
FROM cloud_costs
ORDER BY amount DESC, svc_name ASC
LIMIT 1- Computing the largest value
SELECT MAX(amount) FROM cloud_costsreturns 1784.5 and nothing else. Puttingsvc_namebeside it errors in Postgres and most warehouses; SQLite happens to fill it from the max row, a quirk no reviewer should have to know. AddingGROUP BY svc_namereturns one maximum per service: ten rows here.- Retrieving the largest row
ORDER BY amount DESC, svc_name ASC LIMIT 1never breaks the row apart, sosvc_nameandamountarrive together from one entry.
Common follow-up questions
- What if the analyst wants every entry tied at the top, not just one? (Tests swapping the row cap for a filter against the maximum, or a ranking that keeps ties.)
- How would you return the largest entry per `provider`, given that the column mixes 'aws' and 'AWS'? (Tests scoping the argmax per group and normalizing case before grouping.)
- Should a negative `amount` (a credit) ever change this answer, and when would it start to matter? (Tests reasoning about credits netting against charges once the question moves to totals.)