Tutorials › SQL Tutorial › SQL Aggregate Functions

SQL Aggregate Functions

9 min read · Lesson 13 of 24

Managers rarely want to see every row. They want answers like "how many customers do we have?", "what is our average price?" or "how many units have we sold?". Aggregate functions take many rows and squeeze them into a single value.

The five main aggregate functions

FunctionReturns
COUNT()the number of rows or values
SUM()the total of a numeric column
AVG()the average (mean) of a numeric column
MIN()the smallest value
MAX()the largest value

COUNT

COUNT(*) counts rows. How many customers does Skope Shop have?

Example SQL
SELECT COUNT(*) AS total_customers
FROM customers;
Try it Yourself »

Combine it with WHERE to count only matching rows. How many orders are delivered?

Example SQL
SELECT COUNT(*) AS delivered_orders
FROM orders
WHERE status = 'delivered';
Try it Yourself »

There are three forms of COUNT worth knowing:

FormCounts
COUNT(*)all rows
COUNT(column)rows where column is not NULL
COUNT(DISTINCT column)different non-NULL values in column
Example SQL
SELECT COUNT(*)                AS rows_total,
       COUNT(country)          AS with_country,
       COUNT(DISTINCT country) AS countries
FROM customers;
Try it Yourself »

SUM

SUM() adds up a column. Total units sold across every order line:

Example SQL
SELECT SUM(quantity) AS units_sold
FROM order_items;
Try it Yourself »

You can sum an expression too. The total value of all the stock in the warehouse:

Example SQL
SELECT SUM(price * stock) AS stock_value_ksh
FROM products;
Try it Yourself »

AVG

AVG() calculates the mean. The average product price, and the average price of Accessories only:

Example SQL
SELECT AVG(price) AS avg_price
FROM products;
Try it Yourself »
Example SQL
SELECT ROUND(AVG(price), 2) AS avg_accessory_price
FROM products
WHERE category = 'Accessories';
Try it Yourself »

ROUND(value, 2) rounds to two decimal places, which keeps long decimals out of reports.

Note: Be careful when averaging whole numbers. In some databases (SQL Server, for example) AVG of an integer column returns a whole number, dropping the fraction. SQLite, MySQL and PostgreSQL return a decimal.

MIN and MAX

The cheapest and most expensive product prices:

Example SQL
SELECT MIN(price) AS cheapest,
       MAX(price) AS dearest
FROM products;
Try it Yourself »

MIN and MAX also work on text (alphabetical order) and on dates. The first and most recent order dates:

Example SQL
SELECT MIN(order_date) AS first_order,
       MAX(order_date) AS latest_order
FROM orders;
Try it Yourself »

Aggregates and NULL

All aggregate functions except COUNT(*) ignore NULL values. AVG divides by the number of non-NULL values, not by the number of rows. If an empty table or a filter leaves no rows at all, COUNT returns 0 while SUM, AVG, MIN and MAX return NULL.

A common mistake

Can you show the name of the most expensive product with MAX? You might try:

SELECT name, MAX(price) FROM products;

Most databases reject this, because name is not aggregated: which of 12 names should sit next to a single maximum? (SQLite allows it and happens to pick the matching row, but don't rely on that.) The portable way is a subquery:

Example SQL
SELECT name, price
FROM products
WHERE price = (SELECT MAX(price) FROM products);
Try it Yourself »

You will learn subqueries in Subqueries. To get a total per group (per category, per customer, per month), you need GROUP BY, which is next.

Exercise

Count how many products are in the Accessories category.

SELECT (*) FROM products WHERE category = 'Accessories';
Exercise

Find the lowest stock level of any product.

SELECT (stock) FROM products;
Sign in or create a free account to track progress, earn XP, badges and a certificate.