Skip to main content
All Questions

Remove Duplicate Emails

Medium
Unlock detailed company stats for this questionUpgrade

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:

Related courses

Course

Data Analyst Interview Prep

Ace your data analyst interviews—whether you're targeting product, marketing, or business analyst roles. Tackle real take-home case studies, sharpen your technical and dashboarding skills, and get strategies from interviewers at top tech companies and startups.

Course

Data Science Interview Prep

Land your dream data science role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates. Practice statistics, experimentation, coding, SQL, machine learning, behavioral interviews, and more.