db_select subquery "In" condition causes PDOexception
The only problem with your query is that you're using index as a table alias...this is a MySQL reserved word and although my instinct is that PDO should escape this, perhaps it doesn't.

The following code (with the aliases renamed) runs without error:

$vocab = 2;

$subquery = db_select('taxonomy_term_data','ttd')
  -&gt;fields('ttd',array('tid'));
$subquery
  -&gt;join('taxonomy_term_hierarchy', 'th', 'ttd.tid=th.tid');
$subquery = $subquery
  -&gt;condition('ttd.vid',$vocab)      // vid of vocab we want to show
  -&gt;condition('th.parent',0);  // tid of parent (We want toplevel terms)

$toplvlcats = db_select('taxonomy_index','ti');
$toplvlcats
  -&gt;join('taxonomy_term_data','ttd','ti.tid=ttd.tid');
$toplvlcats = $toplvlcats
  -&gt;fields('ti',array('nid'))
  -&gt;fields('ttd',array('name'))
  -&gt;condition('ti.tid',$subquery,'IN') // Sift through toplevel terms
  -&gt;orderBy('COUNT(nid)','ASC')           // For the one with the most posts
  -&gt;range(0,4)
  -&gt;execute();