"""Easy-tier SQL (SQLite) fixes (written 2026-10-12): two straightforward (`= NULL`, integer division in a percentage), then two subtler ones (a WHERE on the right table of a LEFT JOIN, where adding `OR ... IS NULL` still drops hosts that only have resolved alerts; a join fan-out that COUNT(DISTINCT)/SUM(DISTINCT) only appears to fix, because two hosts share a disk size). Graded by running the query on the fixture with sqlite3 -csv and comparing to "expect".""" SERVERS = """CREATE TABLE servers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, role TEXT NOT NULL, decommissioned_at TEXT -- date the server was retired; NULL while in service );""" BACKUP_RUNS = """CREATE TABLE backup_runs ( id INTEGER PRIMARY KEY, host TEXT NOT NULL, started_at TEXT NOT NULL, status TEXT NOT NULL -- 'ok' or 'failed' );""" ALERTS = """CREATE TABLE hosts ( id INTEGER PRIMARY KEY, name TEXT NOT NULL ); CREATE TABLE alerts ( id INTEGER PRIMARY KEY, host_id INTEGER NOT NULL REFERENCES hosts(id), severity TEXT NOT NULL, -- 'warning' or 'critical' status TEXT NOT NULL -- 'open' or 'resolved' );""" ADDRESSES = """CREATE TABLE hosts ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, site TEXT NOT NULL, disk_gb INTEGER NOT NULL ); CREATE TABLE addresses ( host_id INTEGER NOT NULL REFERENCES hosts(id), ip TEXT NOT NULL, public INTEGER NOT NULL -- 1 = internet-facing address, 0 = internal );""" ITEMS = [ { "id": "sql-null-compare", "lang": "sql", "cat": "bugfix", "context": "The table `servers` is the server inventory; a server is in service until it gets a decommissioned_at date.\n\n```sql\n" + SERVERS + "\n```", "symptom": "The query should list the name and role of every server still in service, ordered by name, but it returns no rows at all (expected db01, db02, web01 and web03).", "fixture": SERVERS + """ INSERT INTO servers VALUES (1, 'web01', 'web', NULL), (2, 'web02', 'web', '2026-03-01'), (3, 'db01', 'db', NULL), (4, 'db02', 'db', NULL), (5, 'mail01', 'mail', '2025-11-15'), (6, 'web03', 'web', NULL), (7, 'db00', 'db', '2024-06-30'); """, "buggy": "SELECT name, role\nFROM servers\nWHERE decommissioned_at = NULL\nORDER BY name;\n", "fix": "SELECT name, role\nFROM servers\nWHERE decommissioned_at IS NULL\nORDER BY name;\n", "expect": "db01,db\ndb02,db\nweb01,web\nweb03,web\n", "ordered": True, }, { "id": "sql-failure-rate", "lang": "sql", "cat": "bugfix", "context": "The table `backup_runs` has one row per nightly backup job run, with its outcome.\n\n```sql\n" + BACKUP_RUNS + "\n```", "symptom": "The failure percentage per host comes out as 0.0 for every host except mail01 (whose only run failed, 100.0); nas01 had 1 failed run out of 3 and should show 33.3, and the hosts should be ordered by that percentage, highest first.", "fixture": BACKUP_RUNS + """ INSERT INTO backup_runs (host, started_at, status) VALUES ('nas01', '2026-10-01 02:00', 'ok'), ('nas01', '2026-10-02 02:00', 'failed'), ('nas01', '2026-10-03 02:00', 'ok'), ('web01', '2026-10-01 02:30', 'ok'), ('web01', '2026-10-02 02:30', 'ok'), ('web01', '2026-10-03 02:30', 'failed'), ('web01', '2026-10-04 02:30', 'ok'), ('db01', '2026-10-01 03:00', 'ok'), ('db01', '2026-10-02 03:00', 'ok'), ('mail01', '2026-10-01 03:30', 'failed'), ('vm07', '2026-10-01 04:00', 'ok'), ('vm07', '2026-10-02 04:00', 'ok'), ('vm07', '2026-10-03 04:00', 'ok'), ('vm07', '2026-10-04 04:00', 'failed'), ('vm07', '2026-10-05 04:00', 'ok'), ('vm07', '2026-10-06 04:00', 'ok'); """, "buggy": "SELECT host,\n COUNT(*) AS runs,\n ROUND(SUM(status = 'failed') / COUNT(*) * 100, 1) AS failed_pct\nFROM backup_runs\nGROUP BY host\nORDER BY failed_pct DESC, host;\n", "fix": "SELECT host,\n COUNT(*) AS runs,\n ROUND(100.0 * SUM(status = 'failed') / COUNT(*), 1) AS failed_pct\nFROM backup_runs\nGROUP BY host\nORDER BY failed_pct DESC, host;\n", "expect": "mail01,1,100.0\nnas01,3,33.3\nweb01,4,25.0\nvm07,6,16.7\ndb01,2,0.0\n", "ordered": True, }, { "id": "sql-open-alerts", "lang": "sql", "cat": "bugfix", "context": "The table `hosts` lists monitored hosts and `alerts` holds every alert raised for them, open or resolved.\n\n```sql\n" + ALERTS + "\n```", "symptom": "The query should list every host with its number of open alerts, ordered by name, but it returns only db01 (1) and web01 (2); hosts with no open alerts, such as fw01, are missing instead of showing 0.", "fixture": ALERTS + """ INSERT INTO hosts VALUES (1, 'web01'), (2, 'web02'), (3, 'db01'), (4, 'nas01'), (5, 'fw01'); INSERT INTO alerts VALUES (1, 1, 'warning', 'open'), (2, 1, 'critical', 'open'), (3, 1, 'warning', 'resolved'), (4, 2, 'critical', 'resolved'), (5, 3, 'warning', 'open'), (6, 4, 'critical', 'resolved'), (7, 4, 'warning', 'resolved'); """, "buggy": "SELECT h.name, COUNT(a.id) AS open_alerts\nFROM hosts h\nLEFT JOIN alerts a ON a.host_id = h.id\nWHERE a.status = 'open'\nGROUP BY h.id, h.name\nORDER BY h.name;\n", "fix": "SELECT h.name, COUNT(a.id) AS open_alerts\nFROM hosts h\nLEFT JOIN alerts a ON a.host_id = h.id AND a.status = 'open'\nGROUP BY h.id, h.name\nORDER BY h.name;\n", "expect": "db01,1\nfw01,0\nnas01,0\nweb01,2\nweb02,0\n", "ordered": True, }, { "id": "sql-public-disk", "lang": "sql", "cat": "bugfix", "context": "The table `hosts` lists servers per site with their disk size, and `addresses` holds each host's IP addresses (a host can have several, or none).\n\n```sql\n" + ADDRESSES + "\n```", "symptom": "Per site, the query should give the number of internet-facing hosts (hosts with at least one public address) and their total disk_gb, but site dal comes out as 3 hosts and 1500 GB when it has only two such hosts (web01 and web02, 500 GB each): expected dal,2,1000.", "fixture": ADDRESSES + """ INSERT INTO hosts VALUES (1, 'web01', 'dal', 500), (2, 'web02', 'dal', 500), (3, 'db01', 'dal', 2000), (4, 'vpn01', 'chi', 100), (5, 'nas01', 'chi', 8000), (6, 'mx01', 'chi', 250), (7, 'lab01', 'hou', 1000); INSERT INTO addresses VALUES (1, '203.0.113.10', 1), (1, '203.0.113.11', 1), (1, '10.0.1.10', 0), (2, '203.0.113.12', 1), (3, '10.0.1.20', 0), (4, '198.51.100.5', 1), (4, '10.1.0.5', 0), (5, '10.1.0.9', 0), (6, '198.51.100.25', 1); """, "buggy": "SELECT h.site, COUNT(*) AS hosts, SUM(h.disk_gb) AS disk_gb\nFROM hosts h\nJOIN addresses a ON a.host_id = h.id\nWHERE a.public = 1\nGROUP BY h.site;\n", "fix": "SELECT h.site, COUNT(*) AS hosts, SUM(h.disk_gb) AS disk_gb\nFROM hosts h\nWHERE EXISTS (SELECT 1 FROM addresses a WHERE a.host_id = h.id AND a.public = 1)\nGROUP BY h.site;\n", "expect": "chi,2,350\ndal,2,1000\n", }, ]