title: How to find least non-null column in one particular row in SQL?
body: <p>I am trying to find the lowest number in two columns of a row in the same table, with the caveat that one of the columns may be null in a particular row. If one of the columns is null, I want the value in the other column returned for that row, as that is the lowest non-null column in this case. If I use the least() function in MySQL 5.1:</p>
  
  <pre><code>select least(1,null)
  </code></pre>
  
  <p>This returns null, which is not what I want. I need the query to return 1 in this case.</p>
  
  <p>I've been able to get the result I want in general with this query:</p>
  
  <pre><code>select least(coalesce(col1, col2)) , coalesce(col2,col1))
  </code></pre>
  
  <p>As long as col1 and col2 are both not null each coalesce statement will return a number, and the least() handles finding the lowest.</p>
  
  <p>Is there a simpler/faster way to do this? I'm using MySQL in this instance but general solutions are welcomed.</p>