WordPress Check If Current Category Has Children

function category_has_children() {
global $wpdb;
$term = get_queried_object();
$category_children_check = $wpdb->get_results(" SELECT * FROM wp_term_taxonomy WHERE parent = '$term->term_id' ");
if ($category_children_check) {
return true;
} else {
return false;
}
}

<?php
if (!category_has_children()) {
//use whatever loop or template part here to show the posts at the end of the line
get_template_part('loop', 'index');
} else {
// show your category index page here
}
?>

Parent Cat ID for single.php

<?php

foreach((get_the_category()) as $childcat) {

$parentcat = $childcat->category_parent;

$the_cat_name = get_cat_name($parentcat);

$the_cat_ID = get_cat_ID($the_cat_name);

}
if($the_cat_ID == 4){
echo "parent cat $the_cat_ID <br />";
}
?>