SQL
Identify Records Without Corresponding Entries in Another Table
Efficiently find 'orphan' records in a primary table that lack matching entries in a related table using an SQL anti-join pattern. Essential for data integrity checks.
SELECT
t1.*
FROM
Table1 t1
LEFT JOIN
Table2 t2 ON t1.id = t2.table1_id
WHERE
t2.table1_id IS NULL;
How it works: This query uses a LEFT JOIN to combine rows from Table1 with matching rows from Table2. By filtering WHERE t2.table1_id IS NULL, it effectively selects only those rows from Table1 for which no corresponding entry was found in Table2. This pattern is commonly known as an anti-join and is useful for finding 'orphan' records or identifying data inconsistencies where a record in one table is expected to have a corresponding entry in another.