Where the Heat Pools

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

Problem

An on-call engineer is checking where CPU load pools across the fleet, working from the per-node readings in infra_nodes. Show each region with its average CPU utilization, listed alphabetically.

Across the fleet, some regions run hotter than others.

Tables

infra_nodes

Columns: node_id INTEGER, hostname TEXT, region TEXT, node_type TEXT, cpu_pct REAL, mem_pct REAL, status TEXT

infra_nodes: 10 of 200 rows
node_idhostnameregionnode_typecpu_pctmem_pctstatus
1037web-02us-west-2memory1319stopped
1074api-01eu-west-1storage2638draining
1111api-02sa-east-1gpu3957offline
1148db-primaryeu-central-1general5276Running
1185db-replicaus-east-1compute6595STOPPED
1222cache-01us-west-2memory7814running
1259worker-01eu-west-1StorageNULLNULLstopped
1296worker-02ap-south-1gpu452draining
1333gpu-node-01eu-central-1general1771offline
1370WEB-03us-east-1compute3090Running

k8s_pods

Columns: pod_id INTEGER, pod_name TEXT, nspace TEXT, status TEXT, restarts INTEGER, cpu_used REAL, mem_used REAL

k8s_pods: 10 of 200 rows
pod_idpod_namenspacestatusrestartscpu_usedmem_used
2043payment-003efproductionPending00.01137
2086search-003f6stagingCrashLoopBackOff00.02274
2129gateway-003fdmonitoringCompleted00.033111
2172worker-00404kube-systemError00.044148
2215cron-0040bdefaultTerminating00.055185
2258ml-serve-00412productionRunning0NULLNULL
2301cache-00419stagingPending00.077259
2344auth-00420monitoringCrashLoopBackOff00.088296
2387payment-00427kube-systemCompleted00.099333
2430search-0042edefaultError00.11370

Expected output

First 5 of 6 rows
regionavg_cpu
ap-south-151.72727272727273
eu-central-145.8235294117647
eu-west-148.94117647058823
sa-east-139
us-east-149.166666666666664

Worked solution and explanation

What this really is

This is a one-table group average wearing a two-table schema as a costume. The schema drops k8s_pods in front of you, but that table carries no region column and no key that points back at a node, so there is nothing to join it on. The whole answer lives inside infra_nodes. The skill being probed is whether you notice that instead of reflexively pattern-matching two tables into a JOIN, and whether you trust AVG to behave when a node is missing its CPU reading. Reach for a join and you either match nothing or fan the rows out; COALESCE a missing reading to 0 and you quietly understate whichever region that node sits in.

Building it

Step 1: Pick the one table that has the answer

Both region and cpu_pct live on infra_nodes. k8s_pods has no region and no shared key, so it contributes nothing to this answer. Start from infra_nodes alone and ignore the decoy.

Step 2: Collapse to one row per region

Group by region and take AVG(cpu_pct). Every node row folds into the average for its region, leaving one row per region.

Step 3: Order the output

The ask is alphabetical, so order by region. The order is part of what gets checked, and each region appears exactly once, so region alone fixes the sequence with nothing left to tie.

Average CPU per region
SELECT region, AVG(cpu_pct) AS avg_cpu
FROM infra_nodes
GROUP BY region
ORDER BY region;

One scan of infra_nodes; k8s_pods never enters the plan.

Joined the decoy table
Joining k8s_pods produces a broken result: there is no region on that table and no key to match on, so you either get nothing back or a meaningless fan-out, and you lose time hunting a data bug that is not there.
Single table
Reading from infra_nodes alone returns every region with its correct average. Fewer rows scanned, no wasted join, and the query says exactly what it means.

Common follow-up questions