You can use Wikidata and its Sparql query language for this. Although it's not straight forward for all countries, e.g. some have especial properties for regional breakdowns that are not defined as ISO standard subregions.
For example it is common to receive historical entities if they are mislabeled, entities that no longer exist because they were merged... it is good for a side project but not suitable for production.