Why Does a SQL Exclusion Report Lose Rows When NULL Appears?

An operations team asks for a list of equipment records that are not in its retirement register. Yesterday the report contained several items. Today it is empty, although nobody retired the entire inventory. Before investigating the dashboard, inspect the exclusion query and the register's missing identifiers. A single unknown value can expose a difference between the business question and the expression used to answer it.
This guide addresses membership exclusion in PostgreSQL. It does not prescribe how every unknown identifier should be treated. That decision belongs in the reporting requirement, because different decisions can produce different legitimate lists.
Identify the three possible comparison outcomes
A comparison involving an unknown value may be unknown rather than true or false. PostgreSQL's subquery documentation explains that NOT IN can return NULL when there is no equal value but the compared list contains NULL. A WHERE condition retains rows for which its condition is true; an unknown result is not a positive selection.
Write the business question without SQL first. For example: “Show equipment with a known identifier for which no retirement record has a matching known identifier.” That wording makes two requirements explicit: the equipment identifier must be known, and the comparison is based on an actual match.
An illustrative three-item register
Use synthetic equipment identifiers 101, 102, and 103. Give the retirement register the values 102 and NULL. The intended selected equipment, under the policy above, is 101 and 103. The NULL retirement row should also appear in a separate data-quality exception list so it is investigated.
Do not quietly remove the unknown record from operational awareness. It may represent equipment that should be retired but cannot yet be matched. The exclusion report and the exception queue answer different questions.
Compare the expression with the policy
For a controlled test, the risky expression is “equipment_id NOT IN (SELECT equipment_id FROM retirements)”. Evaluate the result for each of the three equipment records, including the comparison outcome before the WHERE filter. This intermediate view makes the disappearance explainable.
A candidate expression for the stated policy is “e.equipment_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM retirements r WHERE r.equipment_id = e.equipment_id)”. PostgreSQL documents EXISTS as a test for whether the subquery returns a row. Here, the correlated condition asks whether a matching retirement record exists.
This is an illustrative expression, not a drop-in change for every system. Confirm the actual column types, table aliases, permission boundary, and matching rule with the database owner. An identifier that looks similar after display formatting may still be a different stored value.
Test the outer unknown separately
Add an equipment record whose identifier is NULL. Under the policy stated above it should not enter the selected list. It should enter an exception list. If the team instead wants unidentifiable equipment included for inspection, describe that as a separate branch with a visible label. Do not let a query rewrite make the policy accidentally.
Build a release fixture that exposes the boundary
Keep a small fixture with six cases: a known match, a known non-match, an unknown retirement identifier, an unknown equipment identifier, an empty retirement register, and repeated retirement rows. Write the expected selected equipment beside each case before executing the candidate.
For each test, retain the input rows, expected selected identifiers, actual selected identifiers, and exception identifiers. Comparing counts alone is insufficient: two wrong identifiers can preserve the same total. Have an operations reviewer explain one included item and one excluded item using the plain-language rule.
Also test the real report's date and status filters. A retirement marked “proposed” may need different treatment from one marked “completed.” The NULL correction should not silently change that separate eligibility rule.
Reconcile the first production result
Release through the team's normal controlled change process. Compare the new result against the previous result on a fixed, authorized data snapshot. Classify each difference as explained by the corrected rule, explained by source-data changes, or still unexplained. Investigate the last group before using the list to trigger actions.
Keep the report read-only until the discrepancy is understood. Exporting a list for review and automatically retiring equipment are different operational steps. The evidence that supports one does not automatically authorize the other.
Keep the decision maintainable
Record the unknown-identifier policy next to the report definition, together with the fixture and the responsible owner. A later contributor should be able to tell whether a missing identifier means “exclude,” “include for review,” or “cannot decide.” That documented choice is more durable than remembering that one particular SQL operator once caused an empty report.
Continue with Does an Empty Field Mean Keep, Clear, or Unknown? for the related planning decision.
The published FutureOS introduction describes the project as being in prelaunch. Use this guide to prepare a concrete project discussion; it does not announce an available FutureOS feature.
Prepared with AI assistance. Primary sources checked on October 25, 2026. Examples and procedures are illustrative editorial guidance, not FutureOS product announcements or an independently reviewed assessment.


Comments