SQL: GROUP BY Statements

Aggregate Functions

The Aggregate functions combine all the rows into a single row of data.

AVG

The AVG function returns the combined average of rows.

Syntax:

-- AVG
SELECT AVG(column_1) 
FROM table_name;

Example: Obtain the average amount from the payment table

-- Obtain the average amount from the payment table
SELECT AVG(amount)
FROM payment;
##        avg
## 1 4.200606

MIN

The MIN function returns the minimum value of the rows.

Syntax:

-- MIN
SELECT MIN(column_1) 
FROM table_name;

Example: Obtain the minimum amount from the payment table

-- Obtain the minimum amount from the payment table
SELECT MIN(amount)
FROM payment;
##   min
## 1   0

MAX

The MAX function returns the maximum value of the rows.

Syntax:

-- MAX
SELECT MAX(column_1) 
FROM table_name;

Example: Obtain the maximum amount from the payment table

-- Obtain the minimum amount from the payment table
SELECT MAX(amount)
FROM payment;
##     max
## 1 11.99

SUM

The SUM function returns the combined sum of rows.

Syntax:

-- MAX
SELECT MAX(column_1) 
FROM table_name;

Example: Obtain the total amount from the payment table

-- Obtain the total amount from the payment table
SELECT SUM(amount)
FROM payment;
##        sum
## 1 61312.04