Ordering email addresses by domain name

Alright, so this falls into the silly tips and tricks category but I thought this might be helpful for someone out there. I was sifting thru a users database today and needed to quickly eyeball the domain names being used for each email address. It was only ~1000 records so a quick scroll thru the results was fine. However, because the results were sorted alphabetically it wasn’t easy to get a sense for the frequency of certain domain names.

I thought about using a regex to parse out the domain or some combination of substring and trim, but I just needed a quick solution and in this use case it was also useful to see the entire email address, so I didn’t necessarily want to strip it out.

My “clever” solution? Since I was dealing with a small set of data I just used MySQL’s reverse function

ORDER BY REVERSE(email)

Done! Now all of my email addresses are sorted by domain name and I can quickly scroll thru to see the groupings I need.

Leave a Reply

Your email address will not be published. Required fields are marked *