Compute Nodes in Key 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
Forbes

Problem

Which nodes in infra_nodes run in one of the European regions, 'eu-west-1' or 'eu-central-1', and are working above 70% CPU? Bring back each of those nodes' full record.

Compute nodes across the key regions.

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 18 rows
node_idhostnameregionnode_typecpu_pctmem_pctstatus
1814WEB-03eu-west-1storage8618Running
2073db-replicaeu-central-1general7751STOPPED
2369web-02eu-west-1storage813stopped
2628worker-02eu-central-1general7236draining
2924db-primaryeu-west-1storage7688Running

Worked solution and explanation

What this is really asking

This is a two-condition filter where one condition is a menu of allowed values. Nobody misses the CPU line. What separates candidates is whether the region alternatives stay scoped under it. Write the regions as a loose OR beside the CPU test and precedence quietly hands every idle node in one region to the hot list.

Step 1: Draw the CPU line

cpu_pct > 70 keeps only the nodes working hard. Rows with no reading are NULL, and a NULL is never greater than 70, so they fall out without any extra guard.

Step 2: Express the region set as one condition

region IN ('eu-west-1', 'eu-central-1') is a single predicate that is true for either region. Because it is one predicate, the AND binds it to the CPU check as a unit, which is exactly the meaning the question wants.

Step 3: Return the whole record

The question asks for each node's full record, so SELECT * matches the expected output column for column.

Hot nodes in the European regions
SELECT *
FROM infra_nodes
WHERE cpu_pct > 70
  AND region IN ('eu-west-1', 'eu-central-1')
The precedence bug
SELECT *
FROM infra_nodes
WHERE cpu_pct > 70
  AND region = 'eu-west-1'
   OR region = 'eu-central-1'

AND binds tighter than OR: every node in 'eu-central-1' comes back, whatever its CPU.

Bare OR
Parsed as (cpu_pct > 70 AND region = 'eu-west-1') OR region = 'eu-central-1'. The second region escapes the CPU check.
IN list
region IN ('eu-west-1', 'eu-central-1') is one predicate, so the CPU check applies to both regions. Parenthesizing the OR is the same fix.

Common follow-up questions