SQL
Find Records Without a Matching Entry in Another Table
Identify records in one table that do not have a corresponding entry in a related table, crucial for checking referential integrity and data completeness.
SELECT
t1.*
FROM
table1 AS t1
LEFT JOIN
table2 AS t2 ON t1.id = t2.table1_id
WHERE
t2.table1_id IS NULL;
How it works: This query uses a `LEFT JOIN` to combine `table1` with `table2`. If a record from `table1` does not have a matching `table1_id` in `table2`, all columns from `table2` in that row will be `NULL`. The `WHERE t2.table1_id IS NULL` clause then filters for only those `table1` records that have no corresponding entry in `table2`.