title: MySQL query to group data into different ranges
body: <p>I found a solution for this here,
  <a href="https://stackoverflow.com/questions/6669730/sql-query-that-groups-different-items-into-buckets">sql query that groups different items into buckets</a></p>
  
  <p>It works well as long as there is data for all the ranges. I want the query to return 0 if there is no data.</p>
  
  <p>So if my table is:</p>
  
  <pre><code>item_name | price
  i1        | 2
  i2        | 22
  i3        | 4
  i4        | 26
  i5        | 44
  i6        | 6
  </code></pre>
  
  <p>I want output as:</p>
  
  <pre><code>range   | number of item
  0 - 10  |  3
  11 - 20 |  0
  21 - 30 |  2
  31 - 40 |  0
  41 - 50 |  1
  </code></pre>
  
  <p>The below query will not display result for the 0 count cases.</p>
  
  <pre><code>select
        case when price &gt;= 0 and price &lt;= 10    then "  0 - 10"
             when price &gt; 10 and price &lt;= 20   then " 10 - 20"
             when price &gt; 20 and price &lt;= 30   then " 20 - 30"
             when price &gt; 30 and price &lt;= 40  then " 30 - 40"
             when price &gt; 40 and price &lt;= 50  then " 40 - 50"
             else "over 50"
        end PriceRange,
        count(*) as TotalWithinRange
     from
        YourTable
     group by 1
  </code></pre>
  
  <p>Does anyone has a solution for this?</p>