Applying SQL Aggregate Functions to Online Shop Data
Applying SQL Aggregate Functions to Online Shop Data
Welcome to "Applying SQL Aggregate Functions to Online Shop Data." In this lesson, you'll discover how to leverage SQL functions such as SUM and AVG to analyze online shopping datasets. You'll also explore how the GROUP BY clause can assist in organizing and summarizing data. Through practical examples, you'll gain insights into extracting valuable information from online shop data. Let's get started!
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 analyze data related to online shopping transactions.
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 supports selected or total items bought 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 supports selected per year in the online shopping data.
Example 1: Utilizing The SUM Function
In the above example, we're using the SUM function to find the total supports selected per year in our online shopping data. This is achieved by joining the Orders and OrderItems tables on order_id, where OrderItems records the details of items ordered in each transaction. The GROUP BY clause ensures we get a total supports count for each year, providing a comprehensive view of the transaction trends.
