Regional Footprint

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 capacity plan for 'us-east-1' doesn't match what we believe is deployed there. How many infrastructure nodes sit in that region, whatever state they're in?

Every node costs money. Know what you own.

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

Expected output

First 1 of 1 rows
node_count
40

Worked solution and explanation

What this is really testing

Do not let the one-liner fool you: this is a scoping test wearing a counting problem's clothes. Anyone can type SELECT COUNT(*). What the interviewer watches is whether your count covers exactly the population asked for: every node in 'us-east-1', no more and no fewer. Drop the region filter and you report 200, the whole fleet across six regions, five times the real 40. The subtler miss is the flinch. You notice the messy status column ('Running', 'running', 'STOPPED') and bolt on a running-only clause nobody asked for. That shrinks a footprint headcount to 14, or to 6 if you match lowercase only.


Build it

Step 1: Filter to the region

WHERE region = 'us-east-1' scopes the scan to the primary US region. Because region has no nulls and exact values, an equality predicate is complete. There is no case-folding or trimming to reconcile, the way the status column would demand.

Step 2: Count the survivors

COUNT(*) returns the headcount of surviving rows. Since node_id is the unique, non-null key, one row is one physical node, so counting rows is counting nodes. Alias it to node_count so the result column names itself.


The solution

Filtered count
SELECT COUNT(*) AS node_count
FROM infra_nodes
WHERE region = 'us-east-1'

Common follow-up questions