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
| 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
| 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
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 DESCCommon follow-up questions
- If only two distinct amounts exist in `cloud_costs`, what does this query return? (Tests graceful handling when the data has fewer tiers than requested.)
- How would you adapt this to take an arbitrary N tiers without changing the structure? (Tests generalizing the ranking pattern to a parameter.)
- How would the query change if FinOps wanted the top three tiers per region instead of overall? (Tests partition-aware ranking for a per-group variant.)