Histogram of daily sales in SQL

Question

Suppose you’re given the following tables, showing sales across a 10-day period:

Table: item_sales

day num_sales
1 1055
2 500
3 300
4 1400
5 900
6 1000
7 1100
8 800
9 1350
10 1000

Using this table, write a SQL query to count the number of days where sales fall into bands of 200 (e.g. a histogram). Given the snippet of data in this example, your query should return the following:

bucket count
1000-1200 4
800-1000 2
1200-1400 1
1400-1600 1
200-400 1
400-600 1

Click here to view/query the tables in an interactive SQL fiddle.

Solution

Access restricted

Subscribe to premium account to see the solution.

Get premium now