← Back to Gists

Find Orphan Records

📝 SQL
davidmalone
davidmalone · Level 2 ·

This SQL snippet identifies rows in a table that lack a corresponding foreign key relationship in another table, helping you clean up orphaned data.

SQL
SELECT child.
FROM childtable
LEFT JOIN parenttable ON childtable.parentid = parenttable.id
WHERE parenttable.id IS NULL;

Comments

0
aellis aellis

@christinacrawford @christina_crawford that snippet will miss rows where the foreign key column is NULL since NULL != anything.