global $wpdb;
$categoriesData = $wpdb->get_results("SELECT c.*,ct.* FROM {$wpdb->prefix}terms
as c JOIN {$wpdb->prefix}term_taxonomy as ct ON c.term_id=ct.term_id
WHERE ct.taxonomy='category' ");
foreach($categoriesData as $key=>$category) {
echo $category->name;
}
<?php
global $wpdb;
$cat="2"//Parent category id
$categoriesData = $wpdb->get_results("SELECT c.*,ct.* FROM
{$wpdb->prefix}terms as c JOIN {$wpdb->prefix}term_taxonomy as ct
ON c.term_id=ct.term_id WHERE ct.taxonomy='category'
AND ct.parent ='$cat' ");
?>
<?php
global $wpdb;
$const="4,8,7"; //category Ids
$querystr = "SELECT distinct(wposts.post_title),wposts.ID FROM $wpdb->posts
wposts LEFT JOIN $wpdb->term_relationships ON (wposts.ID =
$wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id =
$wpdb->term_taxonomy.term_taxonomy_id) WHERE $wpdb->term_taxonomy.taxonomy
= 'category' AND $wpdb->term_taxonomy.term_id IN($const) ORDER BY rand() ";
$pageposts = $wpdb->get_results($querystr, OBJECT);
?>