39

I'd like to get a better idea of what domains my customers are using. I could easily do this in PHP by explodeing each address and counting the domain that way. But I'm wondering if there's a way to get this information with just a plain MySQL query?

This is what sample output would look like:

gmail.com | 3942

yahoo.com | 3852

hotmail.com | 209

... and so on, where the first column is the email addresses domain, and the 2nd column is the number of addresses at that domain.

7 Answers 7

103

You would have to do something like this:

SELECT substring_index(email, '@', -1) domain, COUNT(*) email_count
FROM table
GROUP BY substring_index(email, '@', -1)

-- If you want to sort as well:
ORDER BY email_count DESC, domain;
0
4

Adding ORDER BY to WoLpH's answer makes the output more clear:

SELECT substring_index(email, '@', -1), COUNT(*) AS MyCount
FROM `database`.`table`
GROUP BY substring_index(email, '@', -1)
ORDER BY MyCount DESC;
3
  • 1
    Did you miss WoLpH's answer? The same was suggested two years ago ;-)
    – Leigh
    Commented Jun 15, 2012 at 9:17
  • Yes. I added ORDER BY MyCount DESC; I would have loved to add it to his as a commment but do not yet have 50 reputation... I obviously could have made this more clear in my answer.
    – Brad
    Commented Nov 27, 2012 at 22:23
  • Yeah, an explanation might help stave off down votes. Btw, I did not down vote your response since it was valid. I do wish they would relax the whole 50 point restriction thing .. since it frequently produces the opposite of the desired effect. A lot of people do exactly what you did. ie post answers because they cannot comment yet. Catch 22.
    – Leigh
    Commented Nov 27, 2012 at 22:48
1
select distinct SUBSTRING(Email, CHARINDEX('@', Email) + 1,LEN(Email) - CHARINDEX ('@', Email)), Count(*) from Tbl_name
Group by SUBSTRING(Email, CHARINDEX('@', Email) + 1,LEN(Email) - CHARINDEX ('@', Email))
order by Count(*) desc
1

Small tweak to Wolph's original above to shorten a bit and add nice column name and limit results in case list is long. Adjust limit to your own liking

select substring_index(email, '@', -1) AS domain, count(*) from TABLE group by domain order by count(*) DESC limit 40;
0

You can use this query to get Unique count of domain from table.

SELECT substr(email,INSTR(email,"@")+1),count(substr(email,INSTR(email,"@"))) from YOUR_TABLE group by substr(email,INSTR(email,"@"));
0
SELECT 
    substring_index(email_address, '@', -1) AS Domain 
   ,COUNT(*) AS MyCount
FROM 
    database_name.table_name
GROUP BY 
    substring_index(email_address, '@', -1)
ORDER BY
    MyCount DESC
0
-1

How about something like

SELECT COUNT(DISTINCT [what you want])
FROM MyTable

COUNT(DISTINCT expr,[expr...])

1
  • i'm not sure what the expression would be to separate the parts of the email address into user and domain portions.
    – Ian
    Commented Mar 13, 2010 at 23:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.