Menu

SQL Count(), Avg(), Sum() – Unraveling SQL Aggregation Functions Count(), Avg(), Sum()

Written by Jagdeesh | 3 min read

Let’s delve into the wonderful world of SQL (Structured Query Language), focusing on three pivotal functions: COUNT, AVG, and SUM.

If you’re managing databases, these functions are crucial for data analysis and manipulation. But don’t worry if you’re a beginner; we’ll guide you through with easy-to-understand examples and sample data.

Before we begin, let’s set the stage with a simple database table called Orders

sql
CREATE TABLE Orders (
    OrderID INT,
    CustomerID INT,
    Product VARCHAR(50),
    Quantity INT,
    Price DECIMAL(5, 2)
);

INSERT INTO Orders (OrderID, CustomerID, Product, Quantity, Price)
VALUES 
    (1, 1001, 'Widget', 10, 20.00),
    (2, 1002, 'Gadget', 5, 50.00),
    (3, 1001, 'Widget', 20, 20.00),
    (4, 1003, 'Doodad', 15, 10.00),
    (5, 1002, 'Gadget', 10, 50.00),
    (6, 1004, 'Thingamajig', 8, 75.00);

This table stores orders made by different customers for various products, each with their quantity and price.

Input Table: Orders

output
OrderID | CustomerID | Product     | Quantity | Price
--------|------------|-------------|----------|------
1       | 1001       | Widget      | 10       | 20.00
2       | 1002       | Gadget      | 5        | 50.00
3       | 1001       | Widget      | 20       | 20.00
4       | 1003       | Doodad      | 15       | 10.00
5       | 1002       | Gadget      | 10       | 50.00
6       | 1004       | Thingamajig | 8        | 75.00

1) Counting Rows with COUNT()

The COUNT() function returns the number of rows that matches a specified criteria. For example, to know how many orders were made, you would use COUNT() like this

sql
SELECT COUNT(OrderID) AS TotalOrders FROM Orders;

This would return the total number of orders. COUNT(OrderID) counts the number of order IDs, while AS TotalOrders renames the column for the output to be more descriptive.

output
TotalOrders
-----------
6

What if we wanted to know how many orders each customer made? We can use COUNT() with a GROUP BY statement

sql
SELECT CustomerID, COUNT(OrderID) AS OrdersPerCustomer
FROM Orders
GROUP BY CustomerID;

Output:

output
CustomerID | OrdersPerCustomer
-----------|------------------
1001       | 2
1002       | 2
1003       | 1
1004       | 1

2) Averaging Values with AVG()

The AVG() function returns the average value of a numeric column. For instance, if we want to know the average quantity of products per order, we can use the following SQL command

sql
SELECT AVG(Quantity) AS AverageQuantity FROM Orders;

This statement will return the average quantity of all orders.

Output:

output
AverageQuantity
---------------
11.33

To get the average quantity ordered per product, use AVG() with a GROUP BY statement

sql
SELECT Product, AVG(Quantity) AS AverageQuantityPerProduct 
FROM Orders 
GROUP BY Product;

Output:

output
Product     | AverageQuantityPerProduct
------------|--------------------------
Widget      | 15
Gadget      | 7.5
Doodad      | 15
Thingamajig | 8

3) Summing Up with SUM()

The SUM() function returns the total sum of a numeric column. To know the total quantity of products sold, we could use SUM() as such

sql
SELECT SUM(Quantity) AS TotalQuantity FROM Orders;

This statement will return the total quantity of all orders.

Output:

output
TotalQuantity
-------------
68

To calculate the total quantity sold per product, we can combine SUM() with GROUP BY

sql
SELECT Product, SUM(Quantity) AS TotalQuantityPerProduct
FROM Orders
GROUP BY Product;

Output:

output
Product     | TotalQuantityPerProduct
------------|------------------------
Widget      | 30
Gadget      | 15
Doodad      | 15
Thingamajig | 8

A Bonus: Total Sales Per Product

Let’s see how we can combine these functions and other SQL features to get more complex information. For instance, we can calculate the total sales for each product

sql
SELECT Product, SUM(Quantity * Price) AS TotalSales
FROM Orders
GROUP BY Product;

Output:

output
Product     | TotalSales
------------|-----------
Widget      | 600.00
Gadget      | 750.00
Doodad      | 150.00
Thingamajig | 600.00

Wrapping Up

I hope these examples help you understand how to use the COUNT, AVG, and SUM functions in SQL. As we’ve seen, these aggregation functions are potent tools for extracting meaningful insights from your data. They are especially powerful when used with the GROUP BY statement, allowing you to perform calculations on subsets of your data.

Free Course
Master Core Python โ€” Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Jagdeesh
Written by
Related Course
Master SQL โ€” Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Get the full course,
completely free.
Join 57,000+ students learning Python, SQL & ML. One year of access, all resources included.
๐Ÿ“š 10 Courses
๐Ÿ Python & ML
๐Ÿ—„๏ธ SQL
๐Ÿ“ฆ Downloads
๐Ÿ“… 1 Year Access
No thanks
๐ŸŽ“
Free AI/ML Starter Kit
Python ยท SQL ยท ML ยท 10 Courses ยท 57,000+ students
๐ŸŽ‰   You're in! Check your inbox (or Promotions/Spam) for the access link.
โšก Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science professionals use.

๐Ÿ
Core Python โ€” from first line to expert level
๐Ÿ“ˆ
NumPy & Pandas โ€” the #1 libraries every DS job needs
๐Ÿ—ƒ๏ธ
SQL Levels Iโ€“III โ€” basics to Window Functions
๐Ÿ“„
Real industry data โ€” Jupyter notebooks included
R A M S K
57,000+ students
โ˜…โ˜…โ˜…โ˜…โ˜… Rated 4.9/5
โšก Before you go
Python. SQL.
All Free.
R A M S K
57,000+ students  โ˜…โ˜…โ˜…โ˜…โ˜… 4.9/5
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
๐Ÿ”’ 100% free โ˜• No spam, ever โœ“ Instant access
๐Ÿš€
You're in!
Check your inbox for your access link.
(Check Promotions or Spam if you don't see it)
Or start your first course right now:
Start Free Course โ†’
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Zโ„ข: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Zโ„ข: Hands-On Python & R In Data Science

Machine Learning A-Zโ„ข: Hands-On Python & R In Data Science

Machine Learning A-Zโ„ข: Hands-On Python & R In Data Science

Machine Learning A-Zโ„ข: Hands-On Python & R In Data Science

Machine Learning A-Zโ„ข: Hands-On Python & R In Data Science