CMD Guide
HomeDatabasesSQL Practice Problems

Employee Email Verification

The engine walks each email string left-to-right against an anchored regular expression: ^ pins the match to the first character and $ to the last, so the pattern must consume the entire string or the whole row is rejected — there is no "contains a valid-looking part" loophole. Validity is therefore a single boolean: does the full string fit the grammar letter, then any run of allowed username characters, then the literal domain?

Problem

Table Employees(employee_id PK, name, email). Return the rows whose email is valid, in any order. A valid email is a username followed by a fixed domain:

Spec note (corrected here). The original prose said "the domain is @corpexample" but every example and the regex require the full @corpexample.com — Charlie's charlie@corpexample.net is rejected precisely because its domain is not .com. The two statements contradicted each other. The real contract is @corpexample.com; the bare @corpexample wording was a carry-over error and is wrong.

The query (correct version)

SELECT *
FROM   Employees
WHERE  email REGEXP '^[A-Za-z][A-Za-z0-9_.-]*@corpexample\.com$';

Per-token breakdown — read it as one left-to-right machine:

TokenMatchesWhy it is there
^start of stringAnchor. Without it, a valid email buried inside garbage would pass.
[A-Za-z]exactly one letterThe mandatory first character. Rejects .frank@… and 9bob@….
[A-Za-z0-9_.-]one allowed username charThe character class: letters, digits, _, ., -.
*the class, zero or more timesGreedy repeat for the rest of the username. Zero is allowed, so a 1-letter username like a@corpexample.com is valid.
@corpexamplethat literal textThe @ and letters are ordinary characters here.
\.one literal dotEscaped. An unescaped . means "any character," which would also accept @corpexampleXcom.
comthat literal textThe TLD.
$end of stringAnchor. Rejects dave@corpexample.com.evil.com and trailing whitespace.

Note . and - need no backslash inside the class: . is already literal there, and - is literal when it sits last. Only the dot in \.com — outside the class — must be escaped.

Worked trace on the sample rows

The engine evaluates each string against the anchored pattern. Below, the cursor either consumes the whole string (PASS) or hits a character the pattern cannot accept (FAIL).

emailWhat happensVerdict
alice@corpexample.coma→first letter; lice→class*; @corpexample.com→literal; $ at end.PASS
bob123@corpexample.comb→letter; ob123→class* (digits allowed); domain literal matches.PASS
charlie@corpexample.netUsername + @corpexample. match, but the pattern now demands com; it sees net.FAIL — wrong TLD
dave@corpexample.comClean match, cursor reaches $.PASS
eve#corp@corpexample.come→letter; ve→class*; then # is not in the class and is not @corpexample…, so the run stops early and $ is not reached.FAIL — # illegal
.frank@corpexample.comFirst char must be [A-Za-z]; it is .. Match dies at position 1.FAIL — leading dot

Output: rows 101 (Alice), 102 (Bob), 104 (Dave) — exactly the three that fully consumed the pattern.

diagram
diagram

Why the naive version is wrong

A common first attempt skips one or both anchors and uses an unescaped dot:

-- WRONG: no anchors, literal-looking but unescaped dot
WHERE email REGEXP '[A-Za-z][A-Za-z0-9_.-]*@corpexample.com'

Two silent bugs:

The fix is the anchored, escaped form shown above. The lesson generalizes: a validator must be anchored, or it is a substring search wearing a validator's coat.

Portability — the operator is not the same across engines

The pattern is standard POSIX-ish regex, but the syntax to invoke it differs, and this trips people moving a query between databases:

EngineHow to apply the regexNote
MySQL / MariaDBemail REGEXP '^[A-Za-z]…\.com$'REGEXP is case-insensitive by default on many collations; here the class already covers both cases so it does not matter.
PostgreSQLemail ~ '^[A-Za-z]…\.com$'~ = case-sensitive, ~* = case-insensitive. No REGEXP keyword.
SQLiteemail REGEXP '…' only if a REGEXP function is loadedVanilla SQLite has no regex. Either load an extension, or fall back to GLOB/LIKE, which cannot express "one or more of a class" cleanly.
SQL Serverno native regexUse LIKE with limited wildcards, or CLR / SQL Server 2025+ regex functions.

The grader's earlier inline ASCII tables hid this entirely; on a real migration it is the first thing to break.

Pitfalls

Takeaways


Based on the LeetCode-style "Find the Valid Emails" / employee-email-verification problem and the MySQL REGEXP, PostgreSQL ~, and SQLite regex documentation. Anchoring and character-class behaviour follow the POSIX ERE / PCRE references (Friedl, Mastering Regular Expressions). Re-authored and deepened for this guide: resolved the @corpexample vs @corpexample.com spec contradiction in favour of the example data, added a per-token breakdown, a state-machine diagram, a "why the naive version is wrong" note, and the cross-engine portability table.

🤖 Don't fully get this? Learn it with Claude

Stuck on Employee Email Verification? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Employee Email Verification** (Databases) and want to truly understand it. Explain Employee Email Verification from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Employee Email Verification** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Employee Email Verification** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Employee Email Verification** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes