Splitting the Load

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
Visa

Problem

We keep a cost allocation table where each row is one service charge booked to a team. For each team, surface its three highest-cost charges as the team name, a label combining the service and region, and the amount, sorted by team name and then from the largest charge down.

The heaviest bills each team carries.

Tables

cost_allocs

Columns: alloc_id INTEGER, team_name TEXT, svc_name TEXT, region TEXT, amount REAL, period TEXT, acct_id TEXT, category TEXT

cost_allocs: 10 of 200 rows
alloc_idteam_namesvc_nameregionamountperiodacct_idcategory
129data-engS3us-west-283.212026-02acct-101storage
158ml-teamRDSeu-west-1156.422026-03acct-102network
187frontendLambdaap-south-1229.632026-04acct-103database
216backendBigQueryus-central1-120.52026-05acct-104ml
245devopsCloudFronteurope-west1376.052026-06acct-105other
274securityEKSap-southeast-1449.262026-07acct-106compute
303mobileSQSeu-central-1522.472026-08acct-107storage
332PlatformEC2us-east-1595.682026-09NULLnetwork
361DATA-ENGS3us-west-2668.892026-01acct-109database
390platformRDSeu-west-1742.12026-02acct-110ml

Expected output

First 5 of 36 rows
team_nameentry_labelamount
DATA-ENGEC2 - us-west-218058.74
DATA-ENGEKS - eu-central-117147.44
DATA-ENGBigQuery - europe-west116236.14
PlatformSQS - us-east-117967.61
PlatformCloudFront - ap-southeast-117056.31

Worked solution and explanation

What this really is

This is a per-team top-N argmax hiding behind cost reporting. For each team you have to return the three highest-cost rows whole, with their own service and region, not just three numbers. Reach for GROUP BY team_name with MAX(amount) and the aggregation collapses the group. It throws away svc_name and region and only surfaces the single top charge, so you can neither reach the second and third tiers nor build the label. The tempting patch is a correlated amount = (SELECT MAX(amount) ... WHERE team_name = outer.team_name), but it recovers only the first tier. Stretching it to three needs a correlated COUNT(DISTINCT amount) predicate that is gnarlier than the window it is dodging. A self-join back on the max amount also fans out the moment two charges tie at it. ROW_NUMBER caps each team at exactly three rows, so a team whose third-place cost is shared by two services silently loses one of them. Rank the rows within each team and keep every row in the top three tiers: each winner stays intact with its own columns and its ties.


Break down the requirements

Step 1: Rank rows inside each team

Rank every row against the others in its own team with DENSE_RANK() OVER (PARTITION BY team_name ORDER BY amount DESC). Partition on the raw team_name so 'data-eng' and 'DATA-ENG' stay separate teams rather than folding together.

Step 2: Keep the top three cost tiers

Filter the ranked rows to rnk <= 3. DENSE_RANK gives the same rank to every row tied at a cost tier, so this keeps all three tiers and every tied entry within them. ROW_NUMBER would instead cut the group down to exactly three rows.

Step 3: Build the output

Project team_name, the concatenated label svc_name || ' - ' || region AS entry_label, and amount. Then order by team_name, amount descending, and entry_label, which gives a stable, total ordering even when a team has tied entries.


The solution

Rank each team's rows by amount, keep the top three tiers, then build the label
SELECT team_name, svc_name || ' - ' || region AS entry_label, amount
FROM (
    SELECT team_name, svc_name, region, amount,
           DENSE_RANK() OVER (PARTITION BY team_name ORDER BY amount DESC) AS rnk
    FROM cost_allocs
) ranked
WHERE rnk <= 3
ORDER BY team_name, amount DESC, entry_label

Common follow-up questions