Where the Money Sits
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
Problem
The finance team is building a per-team cost summary. For each team, report how many of its allocations fall in the 'compute' category, alongside the total cost across every allocation the team owns, whatever the category.
Every team carries a cost, and some of it is what keeps the servers running.
Tables
cost_allocs
Columns: alloc_id INTEGER, team_name TEXT, svc_name TEXT, region TEXT, amount REAL, period TEXT, acct_id TEXT, category TEXT
| alloc_id | team_name | svc_name | region | amount | period | acct_id | category |
|---|---|---|---|---|---|---|---|
| 129 | data-eng | S3 | us-west-2 | 83.21 | 2026-02 | acct-101 | storage |
| 158 | ml-team | RDS | eu-west-1 | 156.42 | 2026-03 | acct-102 | network |
| 187 | frontend | Lambda | ap-south-1 | 229.63 | 2026-04 | acct-103 | database |
| 216 | backend | BigQuery | us-central1 | -120.5 | 2026-05 | acct-104 | ml |
| 245 | devops | CloudFront | europe-west1 | 376.05 | 2026-06 | acct-105 | other |
| 274 | security | EKS | ap-southeast-1 | 449.26 | 2026-07 | acct-106 | compute |
| 303 | mobile | SQS | eu-central-1 | 522.47 | 2026-08 | acct-107 | storage |
| 332 | Platform | EC2 | us-east-1 | 595.68 | 2026-09 | NULL | network |
| 361 | DATA-ENG | S3 | us-west-2 | 668.89 | 2026-01 | acct-109 | database |
| 390 | platform | RDS | eu-west-1 | 742.1 | 2026-02 | acct-110 | ml |
Expected output
| team_name | compute_count | total_cost |
|---|---|---|
| DATA-ENG | 0 | 179212.3 |
| Platform | 7 | 177568.9 |
| backend | 6 | 161170.57 |
| data-eng | 0 | 175178.1 |
| legal | 1 | 100 |
Worked solution and explanation
What this problem really is
Beneath the finance-dashboard framing, this is two aggregates riding one scan over the same rows: a filtered count of compute allocations sitting next to an unfiltered sum of every dollar. Anyone can sum a column. The move that separates candidates is keeping the 'compute' condition inside the aggregate instead of the WHERE clause. Push it into a WHERE and total_cost silently drops every non-compute row, so finance sees a spend number that is too small with no error to explain it.
Break down the requirements
Step 1: Group by team
GROUP BY team_name produces one row per team, so both metrics land on the same line of the report.
Step 2: Count compute allocations
SUM(CASE WHEN category = 'compute' THEN 1 ELSE 0 END) counts only the compute allocations while still visiting every row. Teams with no compute rows get 0 instead of vanishing.
Step 3: Sum total cost
SUM(amount) runs over the exact same grouped rows, unfiltered, so the total reflects the whole team's spend across every category.
The solution
SELECT
team_name,
SUM(CASE WHEN category = 'compute' THEN 1 ELSE 0 END) AS compute_count,
SUM(amount) AS total_cost
FROM cost_allocs
GROUP BY team_nameCommon follow-up questions
- How would you also show the percentage of a team's allocations that are compute? (Divide `compute_count` by COUNT(*) and multiply by 100, still in a single pass.)
- What if you also needed a count restricted to a recent period window? (Tests moving the condition from a category match to a date-range predicate inside the same CASE.)
- How would you turn this into counts per category across the columns of one row? (Tests one conditional SUM per category to pivot counts into columns.)