Clean Up CRM Email Addresses
Before a CRM export, the sales team wants only contacts whose email passes these rules. "Letter" means A–Z or a–z; "digit" means 0–9.
- It contains exactly one
@. - The part before
@is not empty, starts with a letter, and contains only letters, digits,.,_and-. - The part after
@contains only letters, digits,.and-; it contains at least one., does not start or end with., and never has two dots in a row.
A NULL email is invalid. Return contact_id and email for every valid contact, in any order (no rows if none). SQLite has no REGEXP: use GLOB, LIKE, instr, substr and similar functions.
Tables
crm_contacts — One row per person in the CRM.
| Column | Type | Description |
|---|---|---|
contact_id (key) | INTEGER | Unique contact id |
full_name | TEXT | Contact name |
email | TEXT | Email address exactly as typed by the sales rep; may be NULL |
Example
crm_contacts
| contact_id | full_name | |
|---|---|---|
| 1 | Mara Ortiz | mara.ortiz@fernhill.co |
| 2 | Dev Patel | 9lives@cats.com |
| 3 | Jin Kwan | J_kwan@post.example.org |
| 4 | Sam Reyes | sam@nodot |
| 5 | Lia Moss | two@@fernhill.co |
| 6 | Tom Lee | t-lee99@mail.net |
| 7 | Ivy Chen | plus+tag@x.com |
| 8 | Ola Berg | NULL |
Expected output
| contact_id | |
|---|---|
| 1 | mara.ortiz@fernhill.co |
| 3 | J_kwan@post.example.org |
| 6 | t-lee99@mail.net |
Rows may be returned in any order. Your query also runs against hidden datasets with tricky edge cases.
SQL Path · Strings & CASE · problem 56 of 70 · SQLite
The idea
Without regular expressions, the trick is to break the rules into small checks that GLOB and string functions can each answer. First keep only addresses with exactly one @ (it contains one, but not two), then split them with instr and substr into a local part and a domain.
GLOB character classes do the heavy lifting. local_part GLOB '[A-Za-z]*' means "non-empty and starts with a letter". NOT GLOB '*[^A-Za-z0-9._-]*' means "there is no character outside the allowed set", which is the same as "every character is allowed". The domain gets its own class without _. '?*.?*' requires a dot with at least one character on each side, and three further patterns reject a leading dot, a trailing dot and ...
The alternative uses trim(x, allowed_chars): trimming every allowed character from both ends leaves an empty string only if no forbidden character exists. A NULL email fails every comparison, so it is never returned.
Solution
WITH parts AS (
SELECT contact_id,
email,
substr(email, 1, instr(email, '@') - 1) AS local_part,
substr(email, instr(email, '@') + 1) AS domain
FROM crm_contacts
WHERE email LIKE '%@%'
AND email NOT LIKE '%@%@%'
)
SELECT contact_id, email
FROM parts
WHERE local_part GLOB '[A-Za-z]*'
AND local_part NOT GLOB '*[^A-Za-z0-9._-]*'
AND domain NOT GLOB '*[^A-Za-z0-9.-]*'
AND domain GLOB '?*.?*'
AND domain NOT GLOB '.*'
AND domain NOT GLOB '*.'
AND domain NOT GLOB '*..*';
Another way
SELECT contact_id, email
FROM (
SELECT contact_id,
email,
substr(email, 1, instr(email, '@') - 1) AS loc,
substr(email, instr(email, '@') + 1) AS dom
FROM crm_contacts
WHERE length(email) - length(replace(email, '@', '')) = 1
)
WHERE length(loc) > 0
AND upper(substr(loc, 1, 1)) BETWEEN 'A' AND 'Z'
AND trim(loc, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-') = ''
AND trim(dom, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-') = ''
AND instr(dom, '.') > 0
AND substr(dom, 1, 1) <> '.'
AND substr(dom, -1) <> '.'
AND instr(dom, '..') = 0;
Why common answers fail
A loose LIKE pattern accepts digits at the start, illegal characters, double @ signs and double dots.
SELECT contact_id, email FROM crm_contacts WHERE email LIKE '_%@_%._%';
GLOB is case-sensitive, so [a-z] rejects valid addresses whose first letter is upper case.
SELECT contact_id, email FROM crm_contacts
WHERE email GLOB '[a-z]*@*'
AND email NOT GLOB '*@*@*'
AND substr(email, 1, instr(email, '@') - 1) NOT GLOB '*[^A-Za-z0-9._-]*'
AND substr(email, instr(email, '@') + 1) NOT GLOB '*[^A-Za-z0-9.-]*'
AND substr(email, instr(email, '@') + 1) GLOB '?*.?*'
AND substr(email, instr(email, '@') + 1) NOT GLOB '.*'
AND email NOT GLOB '*.' AND email NOT GLOB '*..*';
Checks one character list for the whole address, so an underscore in the domain slips through.
SELECT contact_id, email FROM crm_contacts
WHERE email GLOB '[A-Za-z]*@?*.?*'
AND email NOT GLOB '*[^A-Za-z0-9@._-]*'
AND email NOT GLOB '*@*@*' AND email NOT GLOB '*@.*'
AND email NOT GLOB '*.' AND email NOT GLOB '*..*';
What the interviewer is checking
Tests whether you can turn a written specification into precise, testable conditions with the limited string tools of a given SQL dialect.
Mistakes to avoid
- Writing [a-z] in GLOB and forgetting upper-case letters (GLOB is case-sensitive, LIKE is not).
- Validating the whole address against one character set, so characters allowed only on one side slip into the other.
- Forgetting that instr returns only the first @, so a second @ ends up inside the domain unless you check for it.
Follow-ups to expect
- Return the invalid addresses with the first rule each one breaks (a CASE expression listing the rules in order).
- Count valid addresses per domain, ignoring case (GROUP BY lower(domain)).
- How would you enforce these rules on insert? (A CHECK constraint using the same expressions.)
In real work
Data quality jobs in CRMs and marketing platforms validate contact fields before syncing to email tools, often in a database without regex support.