Bronze Medal

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
Calix

Problem

The FinOps team is mapping the top spending tiers across cloud usage, where repeated amounts collapse into a single tier. Return the three highest cost amounts, largest first.

Only the top few clear the bar.

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 3 of 3 rows
amount
1784.5
1766.67
1748.84

Worked solution and explanation

What this problem is really about

This is a top-N-with-ties problem wearing a FinOps costume. The real question: can you count spending TIERS instead of rows? Anyone can sort amount descending and grab three rows. The trick is that a tier is a distinct amount, so if the two biggest line items happen to share an amount, LIMIT 3 over the raw table hands you that top amount twice and stops one tier short of the real third-highest value. Get that wrong and FinOps reviews two of the same number as if they were separate tiers.


Building it

Step 1: Number each row by its spending tier

Inside a derived table, attach DENSE_RANK() OVER (ORDER BY amount DESC) to every row. Tied amounts share a tier number, and the counter only advances when the next distinct value appears. That turns 'top three tiers' into a simple predicate on the tier column.

Step 2: Keep the top three tiers and collapse ties

Keep rows where tier <= 3, then SELECT DISTINCT amount so tied line items collapse to a single row per tier, and order the three values largest first. If fewer than three distinct amounts exist, you simply get fewer rows, which is the graceful behavior.


The solution

`DENSE_RANK`, then keep the top three tiers
SELECT DISTINCT amount
FROM (
    SELECT
        amount,
        DENSE_RANK() OVER (ORDER BY amount DESC) AS tier
    FROM cloud_costs
) ranked
WHERE tier <= 3
ORDER BY amount DESC

Common follow-up questions