← Back to Gists

Find Orphan Records

📝 SQL
davidmalone
davidmalone · 6d ago
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 2d ago
@christinacrawford @christina_crawford that snippet will miss rows where the foreign key column is NULL since NULL != anything.