Total Engineering Cost Allocation

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

The CFO is reviewing the cloud allocation total for engineering. Engineering teams in the allocation table are 'data-eng', 'backend', 'devops', and 'platform' (case-insensitive). What is the sum of allocated amounts across those teams?

Engineering's total allocated budget.

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 1 of 1 rows
total_amount
1037131.75

Worked solution and explanation

What this is really asking

This is a case-folded membership filter dressed up as a CFO roll-up. Anyone can write SUM(amount) with an IN list. What separates candidates is noticing that team_name stores the same team under more than one spelling: 'platform' next to 'Platform', 'data-eng' next to 'DATA-ENG'. Match the four names byte for byte and those rows quietly fall out. The total still looks like a perfectly plausible number, and the CFO walks into the review with an engineering figure that is too low.


Break down the requirements

Step 1: Fold the case of team_name

Wrap the column in LOWER() before the comparison so 'Platform' and 'platform' become one value. Fold the column, not the literals: the literals are already lowercase, so folding only them changes nothing.

Step 2: Restrict to the four engineering teams

LOWER(team_name) IN ('data-eng', 'backend', 'devops', 'platform'). 'ml-team', 'frontend', 'security', 'mobile' and the small business teams live in the same table and must stay out.

Step 3: Sum the amount into one number

The CFO wants one figure, so there is no GROUP BY. Keep the negative rows: the -120.5 on 'backend' is a credit, and it nets against the spend exactly as finance books it.


The solution

TOTAL ENGINEERING SPEND
SELECT SUM(amount) AS total_amount
FROM cost_allocs
WHERE LOWER(team_name) IN ('data-eng', 'backend', 'devops', 'platform');
Exact match
WHERE team_name IN ('data-eng', 'backend', 'devops', 'platform') keeps only the rows already spelled in lowercase. Every 'Platform' and 'DATA-ENG' row is dropped, and the total comes out short with no error to warn you.
Case-folded match
WHERE LOWER(team_name) IN (...) compares the folded spelling, so every casing of the four teams lands in the sum and the answer is 1037131.75.

Common follow-up questions

Common follow-up questions