select name, id from ( SELECT COUNTRIES.COUNTRY_NAME name, COUNTRIES.COUNTRY_ID id, count(locations.location_id) FROM LOCATIONS right join COUNTRIES on COUNTRIES.COUNTRY_ID = LOCATIONS.COUNTRY_ID group by COUNTRIES.COUNTRY_NAME, COUNTRIES.COUNTRY_ID order by count(locations.location_id) desc, country_name asc )