Sample Video Frame

Created by Zed A. Shaw Updated 2024-11-15 13:50:32
 

Exercise 18: Aggregate Functions

In this exercise you'll learn the other "aggregate functions". You've already played with count(), so these should be easy as they are used the same.

avg()

The avg() function works exactly like count() does except that it will do a simple average over the column. It is most useful when it's combined with GROUP BY to do summary statistics on different categories of rows. In this exercise I'm going to give you just a simple sample of how avg() works. Then you will study the documentation for avg() and attempt to combine it with what you know so far to produce more interesting queries.

View Source file ex18.sql Only

*/

/* select the average age of every person */
select avg(age) from person;

/* select the average age of every pet */
select avg(age) from pet;

/* select the average age of every breed by dead or alive */
select breed, dead, avg(age) from pet group by breed, dead;
Previous Lesson Next Lesson

Register for Learn SQL the Hard Way

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.