Highest Cost Per Team

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
Forbes

Problem

Our peak-allocation report lists 'platform' and 'Platform' as two different teams, so one team's biggest charge ends up split across two rows. From cost_allocs, give each team's largest single allocation amount, one row per team, biggest peak first.

Peak cost, team by team.

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 14 rows
team_namemax_amount
data-eng18241
platform18149.87
mobile17876.48
security17785.35
devops17694.22

Worked solution and explanation

This is a per-entity maximum with a dirty key. Anyone can write MAX(amount). What the problem actually probes is whether you notice that team_name stores one team under several capitalizations ('platform' and 'Platform', 'data-eng' and 'DATA-ENG') and fold them before you aggregate. Group on the raw column and you get 16 rows for 14 teams: two teams appear twice, and the duplicate with the smaller peak reads like a separate team's number.

Walking to the answer

Step 1: Look at the keys before the metric

Scan the values of team_name first. The sample rows already show 'Platform' and 'platform' side by side, and the full table adds 'DATA-ENG' next to 'data-eng'. Every other team has a single spelling, which is exactly why the bug hides: 12 of the 14 teams look fine.

Step 2: Group on the folded name

Put LOWER(team_name) in the GROUP BY, not just in the SELECT. The grouping key decides which rows compete for one maximum. The selected label only decides what gets printed.

Step 3: Take the peak, then sort by it

MAX(amount) per folded team is that team's single largest allocation. Sorting on max_amount descending puts the biggest peaks on top, which is the order the preview shows.

The solution

Peak allocation per folded team
SELECT LOWER(team_name) AS team_name, MAX(amount) AS max_amount FROM cost_allocs GROUP BY LOWER(team_name) ORDER BY max_amount DESC
Raw label
GROUP BY team_name produces 16 groups. 'platform' and 'Platform' each get their own MAX(amount), so the team shows up twice with two different peaks.
Folded label
GROUP BY LOWER(team_name) produces 14 groups. Every row of a team competes for one maximum, and the team appears once with its true peak.

Common follow-up questions