Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Aggregate functions are functions that operate on a set of rows rather than row by row. For example, we'll be using the world database. And we'll start with a simple example of an aggregate function that we're all familiar with the Count function. So, I'll select COUNT with an asterisk in the parenthesis. FROM the country table. And when I press go, you see there are 239 rows in the country table. So this returns one result for a number of rows.
There's 239 rows but there's only one result for the count. The asterisk is a special case for the count function that will count all the rows in a table. If instead, you specify a column. COUNT will count all the non-null values in that column. So if I put in here population, which I know has a view null values in it, you notice we get a smaller value; instead of 239 we get 232. And those are the non-null values in the population column.
The GROUP BY clause, I'll put an asterisk back in over here and we'll specify a group by may be used to group rows for use with aggregate functions. So if I say, GROUP BY, continent et all. Put continent over here so we can see which continent we're looking at. So this will group our results by continent and give account for each group. And I want to order it by count.
So I'm going to give this an alias with as count,. And I'll order by count descending, so that they're in descending order of count. And so we're gong to get one row in the result for each continent, with a count of how many rows there are in that continent. And it'll be ordered by that count in descending order. So when I press Go here, here's all our continents, Africa, Asia, Europe, North America, et cetera. And there's the count of rows in each of those continents.
So, in this example, the Continent column is used to group the results for the COUNT function. And the COUNT function will apply to each of those groups. And, of course, this grouping will work with any aggregate function. There's one more option that I want to show you. I'm going to use COUNT for this, and again, this works for many of the aggregate functions. I can say COUNT DISTINCT and Continent, and this will actually tell us how many distinct continents there are in this table.
And when I press Go it shows that there are seven distinct values for the continent column. So, what this does it goes through the entire table and it sorts it by continent. And then it only counts each value for continent once, it does not count the duplicates. And that's the distinct keyword inside of the parentheses as the argument for the aggregate function. And again, this works with a lot of the aggregate functions. Another common aggregate function is the GROUP CONCAT function which will concatenate a number of values in a group.
So I can say Select Group, CONCAT, Name, FROM Country. WHERE Region equals Western Europe, and press Go. And here are all the values for the name column where the region equals Western Europe. Now, instead of using this where clause, I can use a GROUP BY clause. Let's see, GROUP BY Region. And now, I get a row for each region with all of the values for name concatenated for that region.
You'll notice that the separator defaults to a common. I can change that by using the separator option, and give it a slash instead. And now, we have slashes instead of commas. I can also use distinct with this. I can say DISTINCT Continent. And I don't need this group by over here anymore. And I can say Go. And I can actually order it by continent. I can say ORDER BY Continent.
And it will change the order of this and put the a's first. So that's the GROUP CONCAT. There are also some numeric functions that are used as aggregates. We have average, and we'll use population for this. So you'll get the average population. Where region equals western Europe. And there's our average population. You can also get the minimum and maximums instead of, or in addition to the average I could say MIN and MAX.
And this will give us the minimum and maximum values for a population that satisfies that. And again, we could do this with GROUP BY instead of the WHERE clause if we wanted to. Or I can get SUMs, which will add them all up. I can even get a standard deviation with STD. And there's our sum and our standard deviation. So aggregate functions are functions that operate on a set of rows rather than row by row. MySQL has a good selection of aggregate functions for working with both strings and numerics.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98513 Viewers
61 Video lessons · 85807 Viewers
71 Video lessons · 69711 Viewers
56 Video lessons · 102023 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.