title: get value distribution via SQL
body: <p>In a MySQL database there's a table with a single numeric <code>value</code> column. I want to plot the distribution of these values as a bar chart. The width of each bar in the chart should be uniform and the height of each bar should reflect the number of entries in this range. </p>
  
  <p>For example, if the range of values is from <code>-10..90</code> and the number of ranges (bars) is 10, then the first bar in the chart should show the number of entries in the range <code>-10..0</code> and the last should show the number of entries in the range <code>80..90</code>.</p>
  
  <p>Is there a better way to calculate the bar chart data than the following:</p>
  
  <ol>
  <li>Get min and max with <code>select min(value), max(value) from mytable</code></li>
  <li>Calculate the upper and lower bounds of each range (in application code)</li>
  <li>Get the number of values in each range with <code>select count(*) from mytable where value between X and Y</code></li>
  </ol>
  
  <p>A problem with this simplistic approach is that a separate query will need to be issued to find the number of values in each range, is it possible to make this more efficient, e.g. by building up the query dynamically and using a <code>GROUP BY</code>?</p>