Compare rows in the same table using inequality to avoid mirrored duplicates.
Section: Self joins
Find duplicate emails with self join
sql
sql
SELECT a.user_id, b.user_id, a.email
FROM users a
JOIN users b
ON a.email = b.email
AND a.user_id < b.user_id;Explanation
The `<` condition avoids returning both `(a,b)` and `(b,a)`.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Self joins
Employee to manager self join
Resolve a manager relationship stored in the same table.
Use self join for one-level hierarchy
Great for direct parent relationships like employee → manager.