SQL: what a CTE is and how to find duplicate records
A CTE (WITH) makes complex queries readable.
1 2 3 4 5 6 7 8 9 10 11 12 13 | -- ====================================================================== -- Q: What is a CTE? How do you find duplicate records? -- -- A CTE (WITH) makes complex queries readable. It is ideal for cleaning legacy -- data before a migration, which I did in practice. After the cleanup I add a -- unique index on lower(email) so it does not happen again. -- ====================================================================== WITH dup AS ( SELECT id, ROW_NUMBER() OVER (PARTITION BY lower(email) ORDER BY id) AS rn FROM customer ) SELECT * FROM customer WHERE id IN (SELECT id FROM dup WHERE rn > 1); |