title: SQL - order by column if value exists, otherwise second column
body: <p>How to order SQL result by column <code>name</code> and if there is not set name, order it by <code>email</code>.</p>
  
  <pre><code>+----+-------+-----------------+
  | ID | name  |      email      |
  +----+-------+-----------------+
  |  1 | John  | john@gmail.com  |
  |  2 | ---   | linda@gmail.com |
  |  3 | ---   | kikli@gmail.com |
  |  4 | Peter | peter@gmail.com |
  +----+-------+-----------------+
  </code></pre>
  
  <p>Result should looks like that:</p>
  
  <pre><code>John, kikli@gmail.com, linda@gmail.com, Peter
  </code></pre>
  
  <p>Answer:</p>
  
  <pre><code>$users = $this-&gt;em-&gt;createQueryBuilder()
          -&gt;select('a.id, coalesce(concat(a.firstName, concat(\' \', a.lastName)), a.email) as orderColumn')
          -&gt;from('Company\User\Admin', 'a')
          -&gt;orderBy('orderColumn','ASC')
          -&gt;getQuery()
          -&gt;getScalarResult();
  </code></pre>