Getting metadata/column names for empty resultset
I was close - I think the problem was the $command-&gt;prepare() did not do anything useful, using $command-&gt;query() executes the query.

This is working for me

    $sql = "select t.foo, count(o.bar) as amount from craft_table t join craft_otherTable o on t.id = o.id where 1=0 group by t.foo;"
    $command = craft()-&gt;db-&gt;createCommand($sql);
    $reader = $command-&gt;query($parameters);
    $statement = $command-&gt;getPdoStatement();
    $result = [];
    $headers = [];
    for($j=0;$j&lt;$statement-&gt;columnCount();$j++){
      $headers[] = $statement-&gt;getColumnMeta($j)['name'];
    }
    $result['headers'] = $headers;
    $result['rows'] = $reader-&gt;readAll();

    // $result['headers'] is now: ['foo', 'amount']
    // $result['rows'] is now: []