I have the following query to select the domain names that have three levels separated by two dots:
select domainname from db.table where criteria like (\'*.com\
You could use the function SUBSTRING_INDEX
ORDER BY SUBSTRING_INDEX(domainname, '.', -2)