Mastering Aggregation: Calculating Totals and Averages in SQL
Quick Recap
Great job on making it this far! Thus far, we've covered a great deal, from drilling into COUNT and DISTINCT to exploring SUM and GROUP BY. These are some of the key SQL functions required to dig deep into any dataset. In this unit, we're going to broaden our repertoire by applying these aggregate functions to our beloved Taylor Swift's music data.
As you may recall from our previous lessons, aggregate functions allow us to perform calculations on a set of values to return a single scalar value. We've already seen the COUNT and SUM functions in action, but have you ever wondered if we could derive other useful insights, such as averages? That’s where the SQL AVG function comes into play.
SUM and AVG Functions
At this juncture, the SUM function must seem pretty familiar to you. It does the heavy lifting when we need to find total values. For instance, it calculates total sales or total song durations in our case.
On the other hand, the AVG function might be new to you. It's a classic SQL function utilized for calculating the arithmetic mean of a set of values. Simply put, AVG can help us determine an average value, such as the average popularity of Taylor Swift's songs.
Example 1: Utilizing The SUM Function
In the above example, we're using the SUM function to find the total duration of each album in our Taylor Swift dataset. This is accomplished by joining the Albums and Songs tables on AlbumID. The GROUP BY clause ensures we get a total duration for each album, rather than for the entire collection of songs.
Example 2: Leveraging The AVG Function
Here, we're introducing the AVG function to find the average popularity of songs in each of Taylor Swift's albums. Much like the previous example, we join the Albums and Songs tables, but this time we use AVG to calculate the average popularity. As always, our trusty GROUP BY clause is ensuring we receive an average popularity score for each album.
