Learn SQL/Intermediate/String Functions/SQL Extract an Email Domain

SQL Extract an Email Domain

A specific, common application of SPLIT_PART — pulling out just the company/provider portion of an email address.

What & Why

Grouping users by email domain — a common way to spot company-wide signups, or filter out free providers like gmail.com — needs exactly the SPLIT_PART(email, '@', 2) pattern from earlier in this series, applied directly.

See How It Works

BUSINESS QUESTION

Marketing wants lead counts by normalized email domain.

idcampaign_idemailcreated_atqualified_atconverted_atlead_scoresourcecountry
3011ana@example.com2024-01-21 09:10:00+002024-01-22 11:00:00+002024-02-02 10:00:00+0086google_adsUS
3021ben@example.com2024-01-24 12:40:00+00NULLNULL52google_adsCA
3032chloe@example.com2024-02-16 08:30:00+002024-02-18 14:20:00+00NULL74emailGB
3043dev@example.com2024-03-20 17:15:00+002024-03-21 09:00:00+002024-04-04 16:00:00+0091linkedinUS
EXAMPLE QUERY
SELECT
  LOWER(SPLIT_PART(email, '@', 2)) AS email_domain,
  COUNT(*) AS leads
FROM marketing.leads
WHERE POSITION('@' IN email) > 1
GROUP BY email_domain
ORDER BY leads DESC, email_domain;
RESULT — exact output from the displayed Queryflo rows
email_domainleads
example.com4

All four valid displayed lead emails share the example.com domain.

Intermediate business practice

Sign up free to try it on a real business scenario