Aggregate Functions with GROUP BY
We have now 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, let's go back to the customers
table. We know the total number of customers is 50,000. But 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;
Once you have the list of states, you could then run the following query for each state:
SELECT COUNT(*) FROM customer WHERE state='{state}'
Although you can do this, it is incredibly tedious and can take an incredibly long time if there are many states. Is there a better way? There is, and it is through the use of the GROUP BY
clause.
GROUP BY
GROUP BY is a clause that divides the rows of a dataset into multiple...