Skip to main content

Remove Duplicate Emails

MediumPremium

Your company has a users table that has accumulated many duplicate email addresses. Some of these duplicates are due to differences in case sensitivity, while others are due to unintentional whitespaces before or after the email. For instance, [email protected] and [email protected] are considered duplicates.

Consider the users table schema:

SQL
CREATE TABLE users ( id INTEGER PRIMARY KEY, email TEXT NOT NULL );

Write a SQL query that returns records from the users table while excluding any duplicate email entries. Your solution should:

  • Treat email addresses as duplicates if they match after accounting for case and whitespace
  • Include only one record for each email, keeping the record with the smallest id.
  • Return the cleaned table, ordered by id.

Example Output: