Which query finds duplicate emails in a users table?
Options
SELECT email, COUNT(*) FROM users GROUP BY email HAVING COUNT(*) > 1(Correct option)SELECT DISTINCT email, COUNT(*) FROM users ORDER BY email DESCSELECT email FROM users WHERE COUNT(email) > 1 GROUP BY emailSELECT email FROM users GROUP BY id HAVING COUNT(email) > 1
Answer
SELECT email, COUNT(*) FROM users GROUP BY email HAVING COUNT(*) > 1
Why
Group rows by email, count how many rows are in each group, and keep only groups with more than one row. HAVING is used because the filter applies to the aggregated result (WHERE cannot use COUNT).
To prevent duplicates in the future, add a UNIQUE constraint on email.
Remember
Duplicates: GROUP BY col HAVING COUNT(*) > 1.