Oracle NOT IN and NULL: Why Your Missing-Customer Report Is Empty
Inspect a tiny Oracle SQL exclusion example, predict the NULL edge case and compare NOT EXISTS before trusting a report.
Why does an exclusion query return no customers?
Suppose you want the IDs of customers who have no recorded payment. Your customer IDs are 101, 102 and 103. The payment export contains 102 and one missing customer ID. A NOT IN filter can unexpectedly return no rows when that missing value enters its subquery.
Oracle's documentation explains the NULL behaviour of NOT IN. The practical lesson is to test missing values before trusting an exclusion report. This is a fictional teaching dataset, not production customer data.
Inspect the boundary case
The following Oracle SQL example is prepared for an Oracle practice environment. It has not been executed on Oracle in this run; the results below are expected results based on the documented semantics.
WITH customers AS (
SELECT 101 customer_id FROM dual
UNION ALL SELECT 102 FROM dual
UNION ALL SELECT 103 FROM dual
), payments AS (
SELECT 102 customer_id FROM dual
UNION ALL SELECT CAST(NULL AS NUMBER) FROM dual
)
SELECT c.customer_id
FROM customers c
WHERE c.customer_id NOT IN (
SELECT p.customer_id FROM payments p
)
ORDER BY c.customer_id;
Expected result: no rows. For customer 101, the non-matching numeric comparison does not resolve the comparison with NULL. A WHERE clause does not retain a row whose condition is UNKNOWN.
Ask the actual business question
Keep the same WITH clause and replace the final SELECT with this version:
SELECT c.customer_id
FROM customers c
WHERE NOT EXISTS (
SELECT 1
FROM payments p
WHERE p.customer_id = c.customer_id
)
ORDER BY c.customer_id;
Expected customer IDs:
101
103
This asks whether a matching payment row exists for the current customer. The payment with a missing ID matches none of these three known customer IDs. It still deserves a separate data-quality check; the query does not repair it.
Practise before an online lesson
Chennai and Tambaram learners can prepare this small case before a SQL demo, share the expected IDs and ask the teaching team to explain the missing-value case step by step. Use a local practice database or an approved training environment. Admission enquiries go through the Tambaram contact; do not upload real customer exports for a sample lesson.
Your exercise
- Remove the NULL row from payments. Both versions should now return 101 and 103.
- Add a second payment for 102. The NOT EXISTS result should still contain only 101 and 103.
- Add a customer with a missing ID. Decide whether the report should retain or reject that row before changing the SQL. A missing customer key is a data-quality decision, not simply a syntax issue.
Limits and next steps
These are single-column numeric keys. Do not extend the conclusion blindly to composite comparisons or use it as a performance claim. Inspect constraints, missing-key policy and execution plans in the actual database. Filtering NULLs out of a NOT IN subquery can be appropriate when that matches the intended business rule; do not hide rejected payment rows from quality monitoring.
Read the Oracle SQL interview guide for related practice. Explore the Oracle SQL and PL/SQL course pathway or request a demo to discuss prerequisites and current learning options.