title: Pros and cons of putting logic in SQL?
body: <p>At a new job, I've just been exposed to the concept of putting logic into SQL statements. </p>
  
  <p>In MySQL, a dumb example would be like this:</p>
  
  <pre><code>SELECT
      P.LastName, IF(P.LastName='Baldwin','Michael','Bruce') AS FirstName
  FROM
      University.PhilosophyProfessors P
  // This is like a ternary operator; if the condition is true, it returns 
  // the first value; else the second value. So if a professor's last name 
  // is 'Baldwin', we will get their first name as "Michael"; otherwise, "Bruce"**
  </code></pre>
  
  <p>For a more realistic example, maybe you're deciding whether a salesperson qualifies for a bonus. You could grab various sales numbers and do some calculations in your SQL query, and return true / false as a column value called "qualifies."</p>
  
  <p>Previously, I would have gotten all the sales data back from the query, then done the calculation in my application code. </p>
  
  <p>To me, this seems better, because if necessary, I can walk through the application logic step-by-step with a debugger, but whatever the database is doing is a black box to me. But I'm a junior developer, so I don't know what's normal.</p>
  
  <p><strong>What are the pros and cons of having the database server do some of your calculations / logic?</strong></p>
  
  <p>**Code example based on <a href="http://orangecow.org/pythonet/sketches/bruces.htm" rel="noreferrer">Monty Python sketch.</a></p>