SQL QuestSQL Interview QuestionsString Functions

Names That Don't Match Their Email

MediumFreeQuerying BasicsString FunctionsJoins

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_idnameemailsignup_datemembershiptotal_orders
1John Smithjohn.smith@email.com2023-01-15Gold15
2Emma Wilsonemma.wilson@email.com2023-03-20Silver8
3Michael Brownmichael.brown@email.com2023-02-10Gold12

Expected output: 3 rows — three accounts that share an address with an older one

Hint

Build the handle by lower-casing the name after replacing each space with a dot. The local part of an email is the substring from position 1 up to one character before the '@' — INSTR gives you where the '@' is. For original_id, LEFT JOIN the table to itself on the same email and a different customer_id, and require the joined row's own handle to equal its own local part.

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

Position of '@' in Email (INSTR)Easy · FreeEmail Provider Customer AnalysisMedium · FreeEmail Username Extract (SUBSTR + INSTR)Medium · FreePassenger Family Survival AnalysisHard · ProHer Own First NameHard · ProShelve the Films Library-StyleHard · Pro

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