Storage Node Lookup

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
City and County of San Francisco

Problem

Find all nodes with a storage-type designation, along with each node's hostname and CPU percentage.

The storage nodes hold the critical data.

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 40 rows
hostnamecpu_pct
api-0126
worker-01NULL
web-0156
db-replica21
WEB-0386

Worked solution and explanation

What this really is

This is a case-folding problem dressed up as a one-line filter. Projecting hostname and cpu_pct from infra_nodes is the easy part. The skill being probed is whether you look at node_type before trusting it. The column was never normalized on write: worker-01 is stored as 'Storage' while every other storage node says 'storage'. Write node_type = 'storage' and you get 39 rows instead of 40. The result looks complete and silently drops a storage node from the inventory.

Step 1: Read the categorical before you filter on it

The sample rows already show 'Storage' sitting next to 'storage'. Whenever a label that drives a filter comes from people or from several writers, assume its casing drifts until the data proves otherwise.

Step 2: Fold node_type, not the literal

LOWER(node_type) = 'storage' maps every spelling to one value, so 'Storage', 'STORAGE' and 'storage' all match. Changing the case of the literal fixes nothing, because the mismatch lives in the column.

Step 3: Project hostname and cpu_pct, and keep the gaps

The filter only touches node_type, so the six storage nodes with no CPU reading, worker-01 among them, come back with a blank cpu_pct. Do not add a cpu_pct IS NOT NULL guard nobody asked for: those nodes are still storage nodes.

Fold `node_type` and project two columns
SELECT hostname, cpu_pct
FROM infra_nodes
WHERE LOWER(node_type) = 'storage'
Exact match
WHERE node_type = 'storage' returns 39 rows. worker-01, stored as 'Storage', disappears and nothing errors.
Folded match
WHERE LOWER(node_type) = 'storage' returns all 40, including worker-01 with its missing cpu_pct.

Common follow-up questions