title: How sum with case conditional statement works in sql
body: <p>The other day, I gave an answer to <a href="https://stackoverflow.com/questions/29316840/get-the-minimum-employees-with-a-given-job/29316973#29316973">this</a> question but then other user solved that problem with sum + case conditional statement to add one edge condition in result. So, the question came to my mind, how statement <code>sum(case when jobname = 'Analyst' then 1 else 0 end)</code> in the below query works  </p>
  
  <pre><code>select d.*
  from (select deptno,
           sum(case when jobname = 'Analyst' then 1 else 0 end) as numAnalysts
        from employees
        group by deptno
        order by numAnalysts asc
       ) d
  where rownum = 1;`
  </code></pre>
  
  <p>and return the number of employees over a department. Also, I would like to understand the performance of this query.</p>
  
  <p>Before posting this question, I read <a href="https://stackoverflow.com/questions/14987534/calculating-a-conditional-cumulative-sum-in-oracle">this</a>, <a href="https://www.google.co.in/?gfe_rd=cr&amp;ei=SogYVfudGOzV8gfPr4DYBA&amp;gws_rd=ssl#q=how+sum+case+statement+works+in+sql+" rel="noreferrer">this</a> and <a href="https://www.google.co.in/#q=sum%20vs%20count%20sql" rel="noreferrer">this</a> but still didn't get how this works.</p>