The derived table accounts for the fact that there are multiple records for each tracking_number. It is summing all the net_amount columns for each tracking_number. Then the rest of the query is trying to generate data to drive a histogram with bins that are 3 digits apart. This is the error I am getting: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mod3 ) AS label FROM ( SELECT b.tracking_number , SUM( b.net_amount ) as net_a'