Nodes in Target Regions

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
Vesta Innovations

Problem

A cross-region latency audit needs the full node record for every infrastructure node in 'us-east-1', 'us-west-2', or 'eu-west-1'.

The target regions need attention.

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 5 of 120 rows
node_idhostnameregionnode_typecpu_pctmem_pctstatus
1037web-02us-west-2memory1319stopped
1074api-01eu-west-1storage2638draining
1185db-replicaus-east-1compute6595STOPPED
1222cache-01us-west-2memory7814running
1259worker-01eu-west-1StorageNULLNULLstopped

Worked solution and explanation

What this really is

This is a set-membership filter dressed up as a latency audit. infra_nodes spans six regions and the audit wants exactly three of them, so the whole problem is saying 'any of these three' as one predicate. Anyone can filter to one region. What separates candidates is writing the membership cleanly and then stopping: write region = 'us-east-1' and you hand back 40 of the 120 nodes, or notice the messy status values, add a filter nobody asked for, and drop the stopped and draining nodes the audit exists to inspect.

Step 1: Look at what region actually holds

The seed has six values: the three targets at 40 nodes each, plus 'ap-south-1', 'eu-central-1' and a single 'sa-east-1'. All lowercase, no padding. That tells you an exact match is safe, so no LOWER or TRIM belongs in this query.

Step 2: Say the three regions as one membership test

region IN ('us-east-1', 'us-west-2', 'eu-west-1') means the same as three equalities joined by OR, but it reads as the list it is and it stays correct when someone later appends AND status = 'running', where a bare chain of ORs would bind the AND to the last region only.

Step 3: Return the full record and filter nothing else

The ask is the full node record, so every column comes back. Region is the only condition: rows with a NULL cpu_pct or mem_pct stay, and 'STOPPED' next to 'stopped' in status is shown as stored, because the audit wants every node in those regions.

One membership filter, full record
SELECT *
FROM infra_nodes
WHERE region IN ('us-east-1', 'us-west-2', 'eu-west-1')
One region
WHERE region = 'us-east-1' runs, looks right and returns 40 rows. Every 'us-west-2' and 'eu-west-1' node is silently missing from the audit.
The three audit regions
WHERE region IN ('us-east-1', 'us-west-2', 'eu-west-1') returns all 120 nodes and leaves out exactly the 80 in the other three regions.

Common follow-up questions