AI basics & verificationPractical AI workflows

Why does an AI-written NOT IN query exclude every row?

First check whether the exclusion list contains SQL NULL. A NOT IN comparison can remain unknown for rows that do not match a known blocked value, and WHERE does not retain unknown results. Replacing NULL with an empty string merely hides a different data question.

Colourful binders and folders arranged on shelves
Illustrative photo: Richard Heinen · Unsplash LicenseStock illustration, not a software screenshot, device review or operation record.

Key Takeaways

The specimen compares currently known blocked IDs and requires a non-null source ID. p.code IS NOT NULL with correlated NOT EXISTS returns A/Ana and C/Cy. Both the missing source ID and the blocklist NULL remain unresolved records; the two rows are a technical reference result, not proof of a complete list or approval to deliver it.

In this article

Define what an acceptable exclusion result means

The query below is an editorial example of an AI-style mistake, not an output attributed to a particular model and not real personnel data. The source has A, B, C and one missing ID. The blocklist contains B and an unresolved NULL. The chosen rule is to deliver only known, nonblocked IDs.

The existing format-and-SQL semantics example examines LEFT JOIN conditions in ON versus WHERE. Here the defect is an unknown value in an exclusion subquery; rearranging formatting or join syntax alone is not the required fix.

The limited rule here compares confirmed, non-null blocked IDs only. The unresolved blocklist record still needs its source owner. If it could represent A or C, a final operational decision must wait for that clarification. A/C are results under the test rule, not proof that a real blocklist is complete or that release is approved.

Run the failing query before blaming export

The read-only CTE can run in a disposable SQLite test environment without creating or changing business tables. The editorial check used Python’s in-memory SQLite and returned zero rows for this fixed input. That is not the intended conclusion that no person is usable.

The SQLite expression reference specifies IN/NOT IN behaviour with NULL. A cannot be established as different from an unknown blocked ID, while B is explicitly blocked. Neither passes this WHERE condition.

Failing query with NULL

WITH people(code, name) AS (
  VALUES ('A','Ana'), ('B','Bo'), ('C','Cy'), (NULL,'Review')
), blocked(code) AS (VALUES ('B'), (NULL))
SELECT p.code, p.name FROM people p
WHERE p.code NOT IN (SELECT code FROM blocked)
ORDER BY p.name;
Authored teaching data; the in-memory SQLite check returned zero rows.

A correlated query still needs a missing-ID policy

The repaired query returns A/Ana and C/Cy. The correlation b.code=p.code checks for an explicit blocked match; IS NOT NULL enforces this article’s source-ID requirement. Without that guard, NOT EXISTS also retains the missing-ID Review row, which violates the chosen delivery rule.

The PostgreSQL subquery reference explains the row-existence interpretation of EXISTS. The execution evidence here is SQLite, not a test of every database. Repeat the boundary cases under the actual dialect, types and constraints before adopting the query.

Reference repair

WITH people(code, name) AS (
  VALUES ('A','Ana'), ('B','Bo'), ('C','Cy'), (NULL,'Review')
), blocked(code) AS (VALUES ('B'), (NULL))
SELECT p.code, p.name FROM people p
WHERE p.code IS NOT NULL
  AND NOT EXISTS (SELECT 1 FROM blocked b WHERE b.code = p.code)
ORDER BY p.name;
Expected: A/Ana and C/Cy only; Review remains a separate unresolved record.

Row-level reference result
Source IDNameDecision
AAnaRetain
BBoExplicit blocked match
CCyRetain
NULLReviewMissing ID; review separately

Accept the result set, not the explanation or indentation

Add two regressions: remove NULL from the blocklist and confirm A/C remain the known-ID result; make the blocklist empty and confirm A/B/C remain, with the missing source ID still isolated by the stated rule. Preserve unknown values instead of inventing a sentinel identifier.

Use the SQL formatter if indentation needs review, but that tool does not execute database queries. Save source, formatted text and actual database output separately. A structurally valid AI answer still needs the source-evidence checks for meaning.

Further reading & sources

Put the guide into practice

Open the relevant tool, preview the result and check your exported file.