SQL Quest › SQL Interview Questions › String Functions
Names That Don't Match Their Email
The CRM team suspects duplicate accounts. Every email in customers follows one house pattern: the customer's name in lower case, the space turned into a dot, then @email.com — John Smith is john.smith@email.com. An account whose name does not produce its own email is a data-quality flag, and usually a second account for someone who already has one.
Show exactly these 5 columns, in this order: customer_id, name, email, name_handle (the name in lower case with every space replaced by a dot), and original_id (the customer_id of the *other* account with the same email whose name *does* produce it — NULL if there is none). Return only the accounts whose name_handle differs from the part of their email before the @. Order by customer_id.
Two string moves do the work: build a handle from the name, cut the local part out of the email, compare the two. Then the table joins to itself on email to find the account the address really belongs to — and that join needs the same handle test, applied to the *other* row.
Mind the punctuation: Emma W. keeps its full stop, so her handle is emma.w. — nothing to strip, and it still does not match.
Solve it in the browser editor →
Runs on SQLite in your browser, graded against the expected result, no signup. A wrong answer gets a diagnosis, not just "incorrect".
Schema
customers
| customer_id | name | signup_date | membership | total_orders | |
|---|---|---|---|---|---|
| 1 | John Smith | john.smith@email.com | 2023-01-15 | Gold | 15 |
| 2 | Emma Wilson | emma.wilson@email.com | 2023-03-20 | Silver | 8 |
| 3 | Michael Brown | michael.brown@email.com | 2023-02-10 | Gold | 12 |
Expected output: 3 rows — three accounts that share an address with an older one
Hint
Concepts
SELECT String Functions SUBSTR INSTR LOWER REPLACE Self-Join LEFT JOIN
Practise the topic: SQL practice questions · String function practice · JOIN practice
Read the concept: SQL cheat sheet
Related questions
Where would this cost you points in an interview?
Ten questions, no signup: a Skillmap across nine SQL skills and the one to fix first.
Take the readiness test