Counting Distinct Domains in Email List

I found this MySQL query that counts the number of emails that have the same domain name in a table. This is handy when you want to check which domain, in your list of emails in a table in the database,  is the most popular or even the top group of domains. My first guess for the list I wanted to check was that aol.com, yahoo.com, hotmail.com and gmail.com would be the top four, and guess what… they were. :)

I editted the query  a little to work on the latest version of MySQL:

SELECT DISTINCT (RIGHT(LCASE(email), LENGTH(email) – INSTR(email, ‘@’))) AS domain, COUNT(email) AS number FROM member
GROUP BY (RIGHT(LCASE(email), LENGTH(email) – INSTR(email, ‘@’))) HAVING (((COUNT(email)) > 1)) ORDER BY number DESC;

Leave a Reply