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
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response