Skip to Main Content
  • Questions
  • Email Domain Extraction using sql query

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, sona.

Asked: June 06, 2016 - 6:45 am UTC

Last updated: July 03, 2018 - 12:37 am UTC

Version: oracle 10.1.2

Viewed 10K+ times! This question is

You Asked

if the part of email before domain matches
for eg in :
xyz@gmail.com
xyzef@gmail.com

if xyz and xyzef does not match ,then do not consider this records

and if the scenario is like ,
if non domain part are equal like
xyz@gmail.com
xyz@gmail.co.in

then retrieve the domain of the both the emails .
o/p should be: gmail.com
:gmail.co.in

and Chris said...

So you want to find all the matches on the local parts of emails (before the domain)?

If so, first you need to extra the this part from the string. You can do this with:

substr(email, 1, instr(email, '@')-1)


Then you just need to return the rows where the count of this is greater than one.

The following uses count as an analytic to get the number of matching local parts:

create table t (
  email varchar2(20)
);

insert into t values ('xyz@gmail.com');
insert into t values ('xyz@gmail.co.in');
insert into t values ('xyzef@gmail.com');


select * from (
  select email, 
         substr(email, 1, instr(email, '@')-1) local_part, 
         count(*) over (
           partition by substr(email, 1, instr(email, '@')-1)
         ) ct
  from   t
)
where  ct > 1;

EMAIL                LOCAL_PART         CT
-------------------- ---------- ----------
xyz@gmail.com        xyz                 2
xyz@gmail.co.in      xyz                 2

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Thanks Chris

sona sh, June 06, 2016 - 10:39 am UTC

Thanks Chris

A reader, July 01, 2018 - 9:26 am UTC

it's fine but i have different names not like 'xyz' in that time how can we count

Connor McDonald
July 03, 2018 - 12:37 am UTC

Notice in our answer - we never referenced "xyz", we simply had:

substr(email, 1, instr(email, '@')-1)


That will count the "before @" part of the email no matter what the value is

More to Explore

Analytics

Analytic SQL got you confused? Check out Connor McDonald's complete video course.