Skip to main content

Calculating Average, Min, and Max with SQL

Premium

SQL provides several mathematical operators that allow you to aggregate data and perform various mathematical calculations. Here's a table of some of the most commonly supported methods:

OperatorDescriptionSyntax
MINReturns the minimum value of a set of values.SELECT MIN(column_name) FROM table_name;
MAXReturns the maximum value of a set of values.SELECT MAX(column_name) FROM table_name;
AVGReturns the average value of a set of values.SELECT AVG(column_name) FROM table_name;

In this lesson, we'll focus on the most commonly used ones in more depth: MIN, MAX, and AVG.

MIN

The MIN operator returns the minimum value of a set of values.

SQL
SELECT MIN(column_name) FROM table_name;

Example

Consider the following table of student grades called grades:

idnamegrade
1Alice90
2Bob85
3Eve80

To find the minimum grade in the table, we could use the following SQL query:

SQL
SELECT MIN(grade) FROM grades;

The result of this query would be:

min
80

MAX

The MAX operator returns the maximum value of a set of values.

SQL
SELECT MAX(column_name) FROM table_name;

Example

Consider the students table from the previous example. To find the maximum grade in the students table, we use the following SQL query:

SQL
SELECT MAX(grade) FROM grades;

The result of this query is:

max
90

AVG

The AVG operator returns the average value of a set of values.

SQL
SELECT AVG(column_name) FROM table_name;

Example

Consider the students table from the previous example. To find the average grade in the students table, we use the following SQL query:

SQL
SELECT AVG(grade) FROM grades;

The result of this query is:

avg
85.0

Try it yourself

Given a table of orders with quantity and price columns, find the average order total.