// SQL snippet

Validate email address shape

Use a coarse LIKE pattern to catch rows that are not even shaped like an email, and list them for correction.

Main statement
All engines

The task

customers.email came from free-text input and a form bug once stored whole sentences. Full RFC validation is overkill; you just want rows that clearly are not emails.

The snippet

Runs unchanged on current versions of SQL Server, PostgreSQL, MySQL, SQLite and Oracle.

All engines
SELECT id, email
FROM customers
WHERE email IS NOT NULL
  AND (email NOT LIKE '%_@_%._%'
       OR email LIKE '% %');

Como funciona

The LIKE pattern '%_@_%._%' demands at least one character before an @, then at least one character, a dot, and at least one character after that dot, and NOT LIKE keeps the rows failing that. The second condition rejects embedded spaces, the most common free-text accident. It is a shape check, not an address-existence check.

Variations

PostgreSQL: one regex for the whole shape

PostgreSQL
SELECT id, email
FROM customers
WHERE email IS NOT NULL
  AND email !~ '^[^@[:space:]]+@[^@[:space:]]+\.[^@[:space:]]+$';

Also reject doubled @ signs

SELECT id, email
FROM customers
WHERE email IS NOT NULL
  AND (email NOT LIKE '%_@_%._%'
       OR email LIKE '%@%@%'
       OR email LIKE '% %');

Questions that come up

Why not validate with a full regex?

Email grammar (RFC 5322) is famously permissive (quoted local parts, comments, long TLDs), so strict regexes both explode in complexity and reject real addresses. For storage hygiene the coarse shape test catches the broken rows; deliverability is the mail server's problem, not the database's.

Keep reading

// snippets are better with a schema brain

Run these with autocomplete that knows your tables

SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.