Use Sophia to knock out your gen-ed requirements quickly and affordably. Learn more
×

Aggregate Functions

Author: Sophia

what's covered
This tutorial explores the different types of aggregate functions that can be used for querying tables:
  1. Introduction
  2. Aggregate Functions

1. Introduction

Most of the queries that we have been working with so far have been focused on one row at a time and looking at individual records. However, a database works using sets of data rather than individual records. There are a lot of questions that can be answered with a database by aggregating the data together as if it were a single unit. This type of processing is done using aggregate functions. Although different databases may have some specialized aggregate functions, there are many common ones that are available in most databases. These aggregate functions take a collection of rows and reduce the resulting set to one row.

SQL can perform various mathematical summaries for us within the database to help answer questions. For example, how many invoices were there in January? Of course we could count, but sometimes counting is not a viable option. We also might want to know what was the smallest invoice total in the prior year, or the maximum invoice total of all time, or the average quantity ordered across all invoices.

2. Aggregate Functions

Aggregate functions can help us answer these questions. Here is a list of the most common aggregate functions. We will get into more specifics on each in later tutorials.


SELECT AVG(total)
FROM invoice;

SELECT COUNT(customer_id)
FROM customer
WHERE country = 'USA';

SELECT MAX(total)
FROM invoice
WHERE invoice_date between '2009-01-01' AND '2010-01-01';

SELECT MIN(total)
FROM invoice
WHERE invoice_date < '2011-01-01';

SELECT SUM(quantity)
FROM invoice_line;
AVG The AVG function calculates the average of non-null values.

As an example, if we were looking for the average of the total items purchased across all invoices, we could run:


AVG Function View
COUNT The COUNT function returns the number of rows in a group including those that have NULL values.

If we wanted to find out how many customers lived in the country USA, we would run:


COUNT Function View
MAX The MAX function returns the largest of the non-null values.

If we wanted to find the largest order made between January 1, 2009 and January 1, 2010, we would run:


MAX Function View
MIN The MIN function returns the smallest of the non-null values.

If we wanted to find the smallest total prior to January 1, 2011, we would run:


MIN Function View
SUM The SUM function returns the sum of all of the non-null values.

If we wanted to find out the number tracks ordered for all time, we would run:


SUM Function View

In PostgreSQL, there are other unique aggregate functions that can be used, including:

BOOL_AND This returns true if all of the input values are true; otherwise, it returns false.
BOOL_OR This returns true if one of the input values is true; otherwise, it returns false.
STDDEV This function returns the standard deviation based on the non-null values.
VARIANCE This function returns the variance of the non-null values.
RANK This function returns the rank of the row based on the value. If a row has the same value as the prior row, it will return the same rank.

There are many others that can be used, but this will be a good starting point to build from.

Video Transcript

try it
Your turn! Open the SQL tool by clicking on the LAUNCH DATABASE button below. Then enter in one of the examples above and see how it works. Next, try your own choices for which columns you want the query to provide.

summary
Aggregate functions allow us to take a collection of rows and reduce the results down to a single row.

Source: Authored by Vincent Tran