Mark As Completed Discussion

Aggregate Functions in Action

In this section, we will work with the table below, named Person:

Aggregate Functions in Action
Our goal is to write a query that takes into consideration all the abovementioned aggregate functions so that we get a better understanding of how they actually work. Let’s say we want to find out the following (code and result table included):

  • The total number of customers (name of the column: NumOfCustomers)
  • The date of the first and last order, based on the date ordered (name of the column: DateFirst & DateLast)
  • The total average age of the customers (name of the column: AverageAge)
  • The total number of items that were being ordered by all customers (name of the column: TotalItems)
TEXT/X-SQL
1SELECT COUNT(Price) AS NumOfCustomers, MIN(DateOfOrder) AS DateFirst, MAX(DateOfOrder) AS DateLast, AVG(Age) AS AverageAge, SUM(NumberOfItems) AS TotalItems
2FROM Person;

The resulting table would look like this:

Aggregate Functions in Action