Skip to content
Astera Technology

Why does NOT IN hide equipment awaiting inspection?

Find equipment with no matched inspection despite an unrelated inspection row with a missing equipment ID.

Download complete case notes and answer keys ↓

Prepare the diagnostic input

The decision is which equipment lacks any matched inspection. NOT IN compares against every subquery value; a NULL can make the nonmatch outcome unknown rather than true. A correlated NOT EXISTS asks whether a matching inspection actually exists and is unaffected by an unrelated missing ID.

Use a fresh SQLite test database for the complete setup below. Every record is fictional; quantities are units and input monetary columns ending in cents use integer currency cents. Calculated averages may include fractions of a cent. Input previews include every table in this small fixture.

Synthetic input: equipment
id
E1
E2
Download editable CSV ↓
Synthetic input: inspections
idequipment_id
1E1
2NULL (not known or not defined)
Download editable CSV ↓
Complete executable setup
CREATE TABLE equipment(id TEXT PRIMARY KEY);
CREATE TABLE inspections(id INTEGER PRIMARY KEY,equipment_id TEXT);
INSERT INTO equipment VALUES ('E1'),('E2');
INSERT INTO inspections VALUES (1,'E1'),(2,NULL);

Reproduce the misleading result and correct it

Misleading query: use for diagnosis only
SELECT id FROM equipment WHERE id NOT IN (SELECT equipment_id FROM inspections) ORDER BY id;
Executed misleading result
Query result
No rows returned (empty result)
Download editable CSV ↓
Corrected SQLite query
SELECT e.id FROM equipment e WHERE NOT EXISTS
(SELECT 1 FROM inspections i WHERE i.equipment_id=e.id) ORDER BY e.id;
Executed corrected result
id
E2
Download editable CSV ↓

The misleading query returns no rows. E1 was inspected, while E2 has no match, so the correction returns E2. The NULL inspection remains a data-quality issue; replacing it with a made-up equipment ID would make neither record trustworthy.

Check the boundary before applying the fix

Remove the NULL row only in a fresh synthetic copy. The corrected queue remains E2, demonstrating that unrelated bad data did not determine equipment eligibility.

For the boundary test, recreate the complete setup in another fresh synthetic database, apply only the change below, then rerun the corrected query. Do not apply a test mutation to a live system.

Boundary change in the fresh fixture
DELETE FROM inspections WHERE equipment_id IS NULL;
Executed boundary result from the corrected query
id
E2
Download editable CSV ↓

Any matched inspection counts here, regardless of date or result. A compliance or recurring-inspection process needs approved interval and pass-status rules.

The displayed bad, corrected and boundary results were executed in SQLite and checked against separately authored expected rows. This demonstrates the stated fixture only; it does not test other database engines, production performance or your live schema.

Related resources