How do you select all rows from one SQL table that do not exist in another table? | A1Lab@HumairaM to SQLEnglish • 2 days ago

How do you select all rows from one SQL table that do not exist in another table?

message-square
0

o select all rows from one SQL table that do not exist in another table, use LEFT JOIN … WHERE right_table.key IS NULL or NOT EXISTS (SELECT 1 FROM right_table WHERE …). In SQL Server (T-SQL), NOT EXISTS and LEFT JOIN … IS NULL are generally preferred over NOT IN, because NOT IN returns zero rows whenever the subquery or value list contains even a single NULL.

  1. Recommended Patterns: NOT EXISTS and LEFT JOIN … IS NULL NOT EXISTS (Safe with NULLs and Clear Intent) NOT EXISTS evaluates true for rows in the left table that have no matching row in the right table. It handles NULL join keys cleanly and frequently maps to an Anti Semi Join in SQL Server execution plans:

SQL SELECT c.customer_id, c.full_name, c.email FROM dbo.customers AS c WHERE NOT EXISTS ( SELECT 1 FROM dbo.orders AS o WHERE o.customer_id = c.customer_id ); LEFT JOIN … WHERE … IS NULL A LEFT JOIN keeps all rows from the left table and sets columns from the right table to NULL when no match exists; filtering WHERE o.customer_id IS NULL isolates unmatched left rows:

SQL SELECT c.customer_id, c.full_name, c.email FROM dbo.customers AS c LEFT JOIN dbo.orders AS o ON c.customer_id = o.customer_id WHERE o.customer_id IS NULL; When checking IS NULL on the right table, pick a column that is non-nullable (NOT NULL) in the right table (such as its primary key or NOT NULL join column)—otherwise a matched row that happens to have NULL in that column looks unmatched.

  1. Why NOT IN Is Risky When NULLs Exist In SQL three-valued logic, x NOT IN (1, 2, NULL) expands to x <> 1 AND x <> 2 AND x <> NULL. Because x <> NULL evaluates to UNKNOWN, the entire WHERE condition becomes UNKNOWN (falsy) for rows that do not match 1 or 2—returning no rows whenever a single NULL appears in the subquery result:

SQL – Avoid when subquery column can be NULL: returns 0 rows if any o.customer_id is NULL SELECT customer_id, full_name FROM dbo.customers WHERE customer_id NOT IN ( SELECT customer_id FROM dbo.orders );

– Safe alternative if using NOT IN: filter out NULLs explicitly (still prefer NOT EXISTS) SELECT customer_id, full_name FROM dbo.customers WHERE customer_id NOT IN ( SELECT customer_id FROM dbo.orders WHERE customer_id IS NOT NULL ); 3. Multi-Column Keys and EXCEPT Matching on Multiple Columns (NOT EXISTS) When uniqueness or correspondence spans two or more columns (such as store_id and sku), match all key columns inside NOT EXISTS:

SQL SELECT s.store_id, s.sku, s.stock_qty FROM dbo.store_inventory AS s WHERE NOT EXISTS ( SELECT 1 FROM dbo.archived_inventory AS a WHERE a.store_id = s.store_id AND a.sku = s.sku ); EXCEPT (Set Difference Across Identical Column Projections) EXCEPT returns distinct rows from the left query that do not appear in the right query, treating NULLs as equal when comparing rows:

SQL SELECT customer_id, full_name FROM dbo.customers EXCEPT SELECT o.customer_id, c.full_name FROM dbo.orders AS o INNER JOIN dbo.customers AS c ON o.customer_id = c.customer_id; Note that EXCEPT deduplicates output rows and requires matching column counts and compatible types across both branches; use NOT EXISTS or LEFT JOIN … IS NULL when you want to preserve full row projection from the left table without EXCEPT set semantics.

  1. Operational Checklist Prefer NOT EXISTS or LEFT JOIN … IS NULL over NOT IN to avoid NULL trap bugs where a single NULL suppresses all results.

Index the join/correlation columns (customer_id, or composite (store_id, sku)) on the target table so NOT EXISTS or LEFT JOIN can use index seeks or hash/merge anti-joins instead of repeated scans.

Test LEFT JOIN … IS NULL against a NOT NULL column of the right-hand table.

Use EXCEPT only for set-style row comparison over aligned column lists where deduplication is desired.