How do I build a dynamic query in Drupal 7 equivalent to this SQL query?
$query = db_select('users', 'u');
$query-&gt;leftJoin('node', 'n', 'n.uid = u.uid');
$query
  -&gt;fields('u', array('uid', 'name',))
  -&gt;groupBy('u.uid')
  -&gt;orderBy('total_written', 'DESC');

// Add the COUNT expression
$query-&gt;addExpression('COUNT(n.id)', 'total_written');
$results = $query-&gt;execute();