runtime
65 / 88

task TRIM / LOWER / SUBSTR / REPLACE · medium

Normalize contact import collisions

Find duplicate valid emails after lowercasing and trimming outer ASCII spaces, tabs, carriage returns and line feeds. Return normalized_email, source_rows and the smallest source id as first_id.

what to return

Columns
normalized_emailsource_rowsfirst_id in this order
Rows
5 rows · in the order asked
Watch for
For this exercise only: exactly one @; a nonempty local part; domain has a dot that is neither its first nor last character; no consecutive domain dots; no ASCII space, tab, CR or LF anywhere after trimming.
This is a deliberately limited import rule, not full email-standard validation. Keep plus aliases distinct; do not remove dots or plus suffixes.
Return only groups with more than one valid source row, ordered by normalized_email.
Skill
Find normalized duplicate contacts while excluding malformed identifiers. Refresher: CAST, text types & NULL arithmetic →

expected result

Your query should return exactly these columns and values. Run it and compare.

expected result5 rows · in this order
normalized_emailsource_rowsfirst_id
ana+sale@harbor.test27
ana@harbor.test21
ben@harbor.test23

first 3 of 5 rows

data you can query

Sample rows from each table. Each column shows its type underneath; → marks a link to another table.

market_raw_import4 columns · 19 rows
idinteger · keyraw_codetextraw_emailtextraw_amounttext
1 hm-0042-us Ana@Harbor.test 19.99
2HM-0007-GBana@harbor.test0.29
3HM-12x4-US Ben@harbor.test -12.50
4HM-12345-US BEN@HARBOR.TEST +12
5HM-123-USa@@harbor.test12x

showing 5 of 19 rows

solution.sql
 

Hints and the answer are always available. Using them counts the question as solved with help.