Node Summary Per 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
Vesta Innovations

Problem

A capacity planner is reviewing the fleet in infra_nodes region by region, where every provisioned node counts toward a region's size whatever its status. Show each region's node count and how many different node_type values appear there, busiest regions first and equally busy ones alphabetically.

Every region has a node story.

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 6 rows
regiontotal_nodesunique_types
eu-central-1401
eu-west-1402
us-east-1401
ap-south-1391
sa-east-111

Worked solution and explanation

This is a rows-versus-values question wearing a capacity-planning badge. Both output columns are counts over the same per-region slice of infra_nodes, and that sameness is the trap. COUNT(*) asks how many nodes, COUNT(node_type) asks how many nodes have a type, and only COUNT(DISTINCT node_type) asks how many different types there are. Regions here hold up to 40 nodes of a single type, so the wrong second count reports 40 types where there is one.

Walk it

Step 1: Slice the fleet by region

One output row per region means GROUP BY region. There is no status filter: a stopped or draining node still occupies capacity, so 'Running', 'STOPPED' and the rest all count. Slip in WHERE status = 'running' and the region totals shrink, which reorders a list meant to show provisioned size.

Step 2: Count rows, then count values

COUNT(*) gives total_nodes. For unique_types the question is about values, so repeats must collapse: COUNT(DISTINCT node_type). In 'eu-west-1' it comes back 2, because one node is recorded as 'Storage' and the rest as 'storage'; the count reports values as recorded.

Step 3: Sort by the count you just named

ORDER BY total_nodes DESC puts the 40-node regions on top. Four regions tie there, so region settles them alphabetically ('eu-central-1', 'eu-west-1', 'us-east-1', 'us-west-2'), then 'ap-south-1' at 39 and 'sa-east-1' at 1 follow.

Node count and type count per region
SELECT
    region,
    COUNT(*) AS total_nodes,
    COUNT(DISTINCT node_type) AS unique_types
FROM infra_nodes
GROUP BY region
ORDER BY total_nodes DESC, region
Counting the column
COUNT(node_type) skips only NULLs, and node_type has none, so it returns 40, 40, 40, 40, 39, 1: the same numbers as total_nodes, a second column that says nothing new.
Counting its values
COUNT(DISTINCT node_type) collapses repeats before counting, returning 1 for every region except 'eu-west-1', which shows 2.

Common follow-up questions