Aggregate Functions with GROUP BY
So far, we've used aggregate functions to calculate statistics for an entire column. However, often, we are not interested in the aggregate values for a whole table, but for smaller groups in the table. To illustrate this, let's go back to the customers
table. We know that the total number of customers is 50,000. However, we might want to know how many customers we have in each state. How would we calculate this?
We could determine how many states there are with the following query:
SELECT DISTINCT Â Â state FROM Â Â customers;
We are expected to see 52 distinct states returned as a result of the preceding query.
Once you have the list of states, you could then run the following query for each state:
SELECT Â Â COUNT(*) FROM Â Â customers WHERE Â Â state='{state}'
Although you can do this, it is incredibly tedious and can take an incredibly long time if there...