Average Node CPU by Region

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
phData

Problem

Capacity planning is sizing up where to add hardware, and only cares about the nodes that are actually running our critical services. Report the average CPU utilization per region across the nodes that have hosted a payment-api, gateway, or auth-svc pod, counting each node once however many such pods it ran.

Average infrastructure node CPU usage broken down by region

Tables

pod_assignments

Columns: assignment_id INTEGER, node_id INTEGER, pod_id INTEGER, svc_name TEXT, assigned_at TEXT, status TEXT

pod_assignments: 10 of 200 rows
assignment_idnode_idpod_idsvc_nameassigned_atstatus
804711482344payment-api2026-02-02 01:11:00draining
809412592645user-svc2026-03-03 02:22:00pending
814113702946search-api2026-04-04 03:33:00terminated
818814813247gateway2026-05-05 04:44:00Active
823515923548notif-svc2026-06-06 05:55:00ACTIVE
828217033849analytics2026-07-07 06:06:00active
832918144150ml-serving2026-08-08 07:17:00draining
837619254451auth-svc2026-09-09 08:28:00pending
842320364752payment-api2026-01-10 09:39:00terminated
847021475053user-svc2026-02-11 10:50:00Active

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

Expected output

First 5 of 5 rows
regionavg_cpu
ap-south-150.42857142857143
eu-central-154.857142857142854
eu-west-158.857142857142854
us-east-140.714285714285715
us-west-254.666666666666664

Worked solution and explanation

The real shape under the capacity question

Under the capacity-planning story this is a membership question: which nodes belong to the average is decided by a filtered lookup into pod_assignments, not by joining to it. Anyone can average cpu_pct by region; the tell is how you gate the node set. Inner-join infra_nodes to pod_assignments and each node fans out once per matching pod, so a node hosting dozens of critical pods is counted dozens of times and the regional average skews toward your busiest boxes. Drop the service filter and you average every node, including ones that never touched a critical workload. The clean move is a semi-join: WHERE n.node_id IN (SELECT node_id FROM pod_assignments WHERE svc_name IN ('payment-api', 'gateway', 'auth-svc')), which keeps each qualifying node exactly once.


Break down the requirements

Step 1: Pin the qualifying node set

A node qualifies if it has ever hosted a critical-service pod, so build that set with a subquery over pod_assignments filtered by svc_name IN ('payment-api', 'gateway', 'auth-svc'). Selecting node_id from that subquery yields the membership list, duplicates and all, which is fine because IN only tests presence.

Step 2: Test membership, do not join

Use WHERE n.node_id IN (...) against infra_nodes rather than an inner join. The semi-join keeps one row per node no matter how many matching pods it ran, so a busy node cannot fan out and drag its region's average upward.

Step 3: Average per region

GROUP BY n.region and take AVG(n.cpu_pct). AVG skips NULL cpu_pct automatically, and there is no filter on either table's status column: hosting a critical pod is the only condition that matters.


The solution

Semi-join filtered to critical services, then average by region
SELECT n.region, AVG(n.cpu_pct) AS avg_cpu
FROM infra_nodes n
WHERE n.node_id IN (
    SELECT node_id
    FROM pod_assignments
    WHERE svc_name IN ('payment-api', 'gateway', 'auth-svc')
)
GROUP BY n.region;

Common follow-up questions