Presence vs. Participation

A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
medium
Seniority
L3
Asked in
Meta

Problem

The platform team is reviewing the machines in the us-east-1 region and wants to see how hard each one is working. For every node in us-east-1, list its hostname and CPU load, and label it 'busy' when CPU is at 50 percent or higher and 'idle' otherwise. Alongside each node, show the current number of active user accounts so demand sits next to the fleet at a glance.

Being in the region and being active are two very different things.

Tables

users

Columns: user_id INTEGER, username TEXT, email TEXT, signup_date TEXT, account_status TEXT, age_bucket TEXT

users: 10 of 200 rows
user_idusernameemailsignup_dateaccount_statusage_bucket
100alicealice@example.com2025-02-02inactive25-34
197aaron42aaron42@example.com2026-03-03suspended35-44
294 ameliaamelia@example.com2024-04-04pending_verification45-54
391arjun arjun@example.com2025-05-05active55-64
488ava99ava99@example.com2026-06-06inactive65+
585andrew_kandrew_k@example.com2024-07-07suspendedNULL
682anikaanika@example.com2025-08-08 16:34:00pending_verification18-24
779aidenaiden@example.com2026-09-09 14:53:00active25-34
876aria_baria_b@example.com2024-10-10inactive35-44
973brianbrian@example.com2025-11-11suspended45-54

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_pctload_stateactive_users
db-replica65busy50
WEB-0330idle50
api-0295busy50
worker-0260busy50
web-0225idle50

Worked solution and explanation

What this really tests

This is a per-row classification with a fleet-wide number bolted onto every row, dressed up as a capacity check. The skill being probed: can you label each infra_nodes row inline AND carry the active-user total next to it, when the two facts live in tables that share no key? Anyone can filter to us-east-1. The trick is that the busy/idle verdict is a CASE on each node's own cpu_pct, while the active-user count is an uncorrelated scalar subquery, so there is nothing to join and nothing to group. Reach for a JOIN and you fan every node out by every active user; reach for a GROUP BY and you hand back one summary row instead of one per machine.


Walk it through

Step 1: See that there is no join

users and infra_nodes share no meaningful key: a user is not deployed on a node. If you join them, each us-east-1 node row multiplies by the number of active users and every count downstream is wrong. Say it out loud in an interview: these tables do not relate, so the active-user figure has to come in as a scalar subquery, not a join.

Step 2: Filter the region and label each node

Scope the listing with WHERE region = 'us-east-1', then label each node with CASE WHEN cpu_pct >= 50 THEN 'busy' ELSE 'idle' END. The boundary is inclusive, so a node sitting at exactly 50 is 'busy'. Writing > 50 here silently flips the label on every 50 percent node, which is the classic off-by-one an interviewer looks for.

Step 3: Carry the active-user total with a scalar subquery

Attach demand with (SELECT COUNT(*) FROM users WHERE account_status = 'active') as its own column. It is uncorrelated, so the engine computes it once and repeats the value on every node row. The account_status = 'active' filter is exact: 'suspended', 'pending_verification' and 'inactive' are not active, and counting them inflates the number.


The solution

Per-row label plus an uncorrelated scalar subquery
SELECT
    hostname,
    cpu_pct,
    CASE WHEN cpu_pct >= 50 THEN 'busy' ELSE 'idle' END AS load_state,
    (SELECT COUNT(*) FROM users WHERE account_status = 'active') AS active_users
FROM infra_nodes
WHERE region = 'us-east-1'

Common follow-up questions