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

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 3 of 3 rows
providersecond_highest
aws1748.84
gcp1713.18
azure1641.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.

Second highest cost per provider
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 DESC

On this data the result is 3 rows: 'aws' at 1748.84, 'gcp' at 1713.18 and 'azure' at 1641.86.

5 spellings, 3 providers

Grouping on the raw spelling
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 DESC

Returns 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.provider compares 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 amount descending 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 < peak has no notion of position. Every row tied at the top fails the test together, and the next distinct amount wins.

Common follow-up questions