intermediateTop 30 Scenario-Based Questions
A table has duplicate email rows due to a data quality issue. How do you delete duplicates keeping only the most recent?
DELETE FROM employees WHERE emp_id IN (SELECT emp_id FROM (SELECT emp_id, ROW_NUMBER() OVER (PARTITION BY email ORDER BY created_at DESC) AS rn FROM employees) ranked WHERE rn > 1); How it works: ROW_NUMBER assigns 1 to the most recent row per email (rn=1), 2+ to older duplicates. DELETE removes all rows where rn > 1 (the duplicates). After deletion: ADD UNIQUE CONSTRAINT to prevent future duplica
This is a Pro chapter
Sign in, then upgrade to Pro or Power to unlock this and the full Databases Mastery library.
A table has duplicate email rows due to a data quality issue. How do you delete duplicates keeping only the most recent?