title: Difference between if and when statements in SQL
body: <p>If I have a table with a <code>number</code> column and want to create a new column with values of either 'high', 'medium' or 'low' based on the value in the number column can I use an if/else statement to do this? If so how would this be written?</p>
  
  <p>I have this code for creating the column using a when/then statement:</p>
  
  <pre><code>use master
  
  select 
      *, 
      case
         when (number &gt; 40) then 'high'
         when (number between 30 and 40) then 'medium'
         else 'low'
      end as 'newColumn'
  from 
      myTable
  </code></pre>
  
  <p>Basically I'm trying to understand the difference between if/else and when/then statements and why one would be used over the other.</p>
  
  <p>This if/else code doesn't work but I don't know if it's because I'm coding it incorrectly:</p>
  
  <pre><code>use master
  
  select 
      *,
      if (number &gt; 40)
        'high'
      else if (number between 30 and 40)
        'medium'
      else 'low'
  from 
      test
  </code></pre>