How to delete true orphaned taxonomy terms
I wouldn't advise doing that - taxonomy terms are entities and need to be cleaned up with taxonomy_term_delete(). Otherwise you'll potentially leave a lot of orphaned data in field tables/contrib module tables/etc.

However, if you know your DELETE query will get you the correct terms, just convert it to a SELECT and you can do things the proper way:

$tids = db_query('SELECT tid FROM taxonomy_term_hierarchy WHERE parent NOT IN (SELECT tid FROM taxonomy_term_hierarchy)')-&gt;fetchCol();

foreach ($tids as $tid) {
  taxonomy_term_delete($tid);
}


Be wary that you don't delete top-level terms that have a parent of 0, since that record will not appear in your sub-query. It'll probably need a bit more thought, but the above code won't be too far off what you end up using.