Creating a Histogram with SQL

sql

In data analysis, histograms are often used to visualize the distribution of a single variable. In this post, we'll look at an SQL query that creates a histogram, given a number of bins (in this case, 10). This SQL code can be adapted to work with any table and any data. For our example, we'll use a table named "weather", with a column called "temperature".

select
min(data.temperature) as min,
max(data.temperature) as max,
count(data.temperature) as count,
floor(
(
case when data.temperature = value.max then value.max*0.999999999 else data.temperature end
- value.min
) / (
value.max - value.min
) * 10
) +1 as bin_number
from "weather" as data
cross join (select max(temperature) as max, min(temperature) as min from "weather") as value
where data.temperature is not null
group by bin_number
order by bin_number;
minmaxcountbin_number
12.714.9811
1517.23202
17.319.53713
19.621.83674
21.924.12255
24.226.41056
26.528.7567
28.831358
31.133.1109
33.635.6710

Quick explanation of the code:

You can play around with the code and see the results in this SQLite Playground.