Find the Fifth Largest Cost

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
Asana

Problem

Our cost reviews bucket line items into spending tiers, one tier per billed amount, so line items billed the same amount share a tier. Pull every line item in cloud_costs that falls in the five highest tiers.

Not the biggest. Not the smallest. The fifth.

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
cost_idprovidersvc_nameregionamountacct_idbill_date
4628GCPBlob Storageus-east-11713.18acct-10162026-07-01
4671AzureCosmos DBus-west-21731.01acct-10172026-08-01
4714awsCloudFronteu-west-11748.84acct-10182026-09-01
4757gcpPub/Subap-southeast-11766.67NULL2026-01-01
4800AWSEC2us-central11784.5acct-10002026-02-01

Worked solution and explanation

Tiers, not rows

This is a dense-rank cutoff dressed up as a cost review. The skill being probed: can you find where the fifth distinct amount sits when the top of the table is full of repeats? Anyone can sort by amount descending. The trick is noticing that a tier is a value, not a row, so two line items billed '1713.18' are one tier. Count rows instead and duplicates at the top eat your five slots: you stop early, report the wrong cutoff, and silently drop line items that belong in the review.


Building it

Step 1: Pin the fifth tier's amount

For each candidate amount c1.amount, count how many distinct amounts sit strictly above it with COUNT(DISTINCT c2.amount). The fifth tier is the amount with exactly 4 above it. DISTINCT here is the whole problem: without it, repeated top amounts inflate the count and the fifth tier never gets found.

Step 2: Collapse it to one cutoff

Wrap that as a scalar subquery so it resolves to one number, the cutoff. SELECT DISTINCT c1.amount matters because several rows can carry the fifth tier's amount and a scalar subquery must hand back a single value.

Step 3: Filter by value, not position

Keep every line item with amount >= cutoff. Every amount at or above the fifth tier belongs to one of the top five tiers by construction, and every tied row comes along because the filter is on the value, not on a position.

Every line item in the top five tiers
SELECT cost_id, provider, svc_name, region, amount, acct_id, bill_date
FROM cloud_costs
WHERE amount >= (
  SELECT DISTINCT c1.amount
  FROM cloud_costs c1
  WHERE 4 = (SELECT COUNT(DISTINCT c2.amount) FROM cloud_costs c2 WHERE c2.amount > c1.amount)
)

The cutoff is found once; the outer filter keeps all ties at and above it.

Counting rows
ORDER BY amount DESC LIMIT 5 returns five rows. If the biggest amount repeats three times, three of your five slots are one tier, and the real fourth and fifth tiers never appear.
Counting amounts
COUNT(DISTINCT c2.amount) treats each amount once, so the cutoff lands on the fifth distinct value and amount >= cutoff returns however many rows those five tiers actually hold.

Common follow-up questions