In the Shadow of the Peak
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
- Instacart
Problem
The FinOps team already knows each provider's single most expensive line item and now wants the next cost sitting just below it. Return that runner-up amount for every cloud provider, biggest runner-up first.
Every provider has a top-line cost. Find the one right behind it.
Tables
cloud_costs
Columns: cost_id INTEGER, provider TEXT, svc_name TEXT, region TEXT, amount REAL, acct_id TEXT, bill_date DATE
| 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
| provider | second_highest |
|---|---|
| aws | 1748.84 |
| gcp | 1713.18 |
| azure | 1641.86 |
Worked solution and explanation
The runner-up is the largest amount that is strictly smaller than a provider's peak. Read that way, the question is 2 aggregates stacked: find each provider's MAX(amount), keep only the rows below it, and take MAX(amount) over what is left. What the sample rows warn you about is the definition of a provider.
The max below the max
Step 1: Decide what a provider is
The sample rows show 'GCP' and 'gcp', 'AWS' and 'aws', plus 'Azure': 5 spellings for 3 providers. The expected output is lowercase with 3 rows, so LOWER(provider) is the key everywhere the provider is used.
Step 2: Find each provider's peak
The correlated subquery returns MAX(amount) for the outer row's provider. Its predicate compares LOWER(c2.provider) to LOWER(c.provider), so an 'AWS' row is measured against the peak across both AWS spellings.
Step 3: Keep what sits strictly below
amount < (...) removes the peak row and every row tied with it. That matches the request: the next cost below the top, even when the top amount was billed more than once.
Step 4: Take the max again
Group the surviving rows by LOWER(provider); their MAX(amount) is the runner-up. ORDER BY second_highest DESC puts the biggest runner-up first.
SELECT LOWER(provider) AS provider,
MAX(amount) AS second_highest
FROM cloud_costs c
WHERE amount < (
SELECT MAX(amount)
FROM cloud_costs c2
WHERE LOWER(c2.provider) = LOWER(c.provider)
)
GROUP BY LOWER(provider)
ORDER BY second_highest DESCOn this data the result is 3 rows: 'aws' at 1748.84, 'gcp' at 1713.18 and 'azure' at 1641.86.
5 spellings, 3 providers
SELECT provider,
MAX(amount) AS second_highest
FROM cloud_costs c
WHERE amount < (
SELECT MAX(amount)
FROM cloud_costs c2
WHERE c2.provider = c.provider
)
GROUP BY provider
ORDER BY second_highest DESCReturns one row per spelling: 5 rows where the answer has 3.
Each of those 5 rows is a runner-up inside a single spelling. 'AWS' and 'aws' each report their own number, and neither is guaranteed to be the provider's.
- `LOWER` only in `GROUP BY`
- Leaving the subquery as
c2.provider = c.providercompares each row to the peak of its own spelling. The top row of the smaller spelling is filtered out as a peak, and that row can be the provider's true runner-up. You get 3 tidy rows with a number that can be too low. - `LOWER` in the correlation too
- With
LOWER(c2.provider) = LOWER(c.provider)there is one peak per provider. Only rows tied with that peak are removed, so the top row of the other spelling stays eligible.
Ties and NULLs at the top
- `ROW_NUMBER()` row 2
- Numbering rows by
amountdescending and taking row 2 returns the peak again whenever the top amount appears on 2 line items. It also has to decide where NULL amounts sort. - `amount` below the peak
amount < peakhas no notion of position. Every row tied at the top fails the test together, and the next distinct amount wins.
Common follow-up questions
- What comes back for a provider whose line items all carry the same
amount?No row at all, because nothing sits below the peak and the group never forms; build the provider list separately and
LEFT JOINthe runner-ups to it if the team wants a NULL shown. A newly onboarded provider with a single invoice would otherwise vanish from the report without anyone noticing. - How would you change this to return the Nth highest cost per provider?
Rank with
DENSE_RANK()partitioned byLOWER(provider)and ordered byamountdescending, after removing NULL amounts, then keep the rank you want. Nesting a max below a max stops being readable past the runner-up, and dense ranking keeps the same tie behavior. - Some amounts are negative. Should they be in scope?
10 rows carry a negative
amount, and they sit far below these runner-ups, so the answer does not move. Ask what they are (credits, refunds) before anyone reuses this query for the lowest costs, where they would take over the result.