How to Work with Aggregate Functions
Besides column data and calculated fields you can also display the results of aggregate functions on an SQL select statement. An aggregate function derives it value from an aggregation, or group, of values. For example, we might wish to sum the populations of major cities in California or display the average of the population of major cities in Florida.
To learn work with aggregate functions in a SQL select statement, follow these steps:
- You'll need to setup the MySQL database tables. The instructions for the setup can be found in How to add comments in simple SQL selects. Follow steps 1 through 7 before proceeding to the next step.
- We turn our attention to working with aggregate functions.
- First, let's compute the sum of the populations of major California (state abbreviation = 'CA') cities. Execute the following statement:
sumaggregate function accepts as its argument the column value we wish to sum across the selected rows. The selected rows form the group or aggregation.
- Next, we will compute the average population of major Florida (state abbreviation = 'CA') cities. Execute the following statement:
avgaggregate function accepts as its argument the column value we wish to average across the selected rows. As before, the selected rows form the group or aggregation.