Imagine your school has a database containing the examination scores of 500 students. Your teacher wants to know:
You do not need to calculate everything manually. SQL has special tools called functions that can perform these calculations automatically.
In this module, you will learn how to use COUNT(), SUM(), AVG(), MIN(), MAX(), GROUP BY, and HAVING.
By the end of this module, you should be able to:
A function is a built-in instruction that helps a computer perform a particular task.
In SQL, functions help us perform calculations and work with information stored in database tables.
For example:
These five functions are known as aggregate functions because they perform calculations across a set of records and return a single result for that set.
COUNT() helps you find the number of records or values in a database table.
Imagine a Students table containing the following information:
| StudentID | Name | Score |
|---|---|---|
| 1 | Ade | 75 |
| 2 | Bola | 90 |
| 3 | Chika | 65 |
| 4 | David | 85 |
| 5 | Esther | 80 |
To count all the students, use:
SELECT COUNT(*) AS TotalStudents
FROM Students;
Explanation:
To count students who have a recorded score, use:
SELECT COUNT(Score) AS StudentsWithScores
FROM Students;
COUNT(column) counts only values in that column that are not NULL. NULL means a value is missing or unknown.
SUM() adds numerical values together.
For example, a school wants to calculate the total examination scores of all students.
SELECT SUM(Score) AS TotalScore
FROM Students;
SQL adds the scores in the Score column and returns the total.
Imagine a supermarket has a Products table containing the prices of different products.
| ProductID | ProductName | Price |
|---|---|---|
| 1 | Rice | 75000 |
| 2 | Beans | 45000 |
| 3 | Vegetable Oil | 12000 |
| 4 | Milk | 8500 |
To calculate the total of the listed product prices, use:
SELECT SUM(Price) AS TotalPrice
FROM Products;
This query adds the numerical values in the Price column. The result is 140500.
Note: This example adds listed prices. It does not calculate actual sales revenue.
AVG() calculates the average of numerical values.
An average is calculated by adding the numbers together and dividing the total by the number of values.
For example, consider these examination scores:
To calculate their average score, use:
SELECT AVG(Score) AS AverageScore
FROM Students;
SQL calculates the average of the non-NULL values in the Score column. In this example, the average is 79.
AVG() is useful when calculating average examination scores, average product prices, and average sales values.
MIN() and MAX() help you find the smallest and largest values in a column.
MIN() returns the smallest non-NULL value in a column.
SELECT MIN(Score) AS LowestScore
FROM Students;
This query finds the lowest examination score in the Students table.
MAX() returns the largest non-NULL value in a column.
SELECT MAX(Score) AS HighestScore
FROM Students;
This query finds the highest examination score in the Students table.
You can use several functions in one SQL statement.
SELECT
MIN(Score) AS LowestScore,
MAX(Score) AS HighestScore,
AVG(Score) AS AverageScore
FROM Students;
This query returns the lowest score, highest score, and average score in one result.
Sometimes, you may want to calculate information for different groups instead of calculating one result for the entire table.
For example, a school wants to calculate the average examination score for each class.
Consider this Students table:
| Name | Class | Score |
|---|---|---|
| Ade | JSS1 | 70 |
| Bola | JSS1 | 80 |
| Chika | JSS2 | 65 |
| David | JSS2 | 85 |
| Esther | JSS3 | 90 |
Use GROUP BY to arrange students into groups based on their classes.
SELECT
Class,
AVG(Score) AS AverageScore
FROM Students
GROUP BY Class;
This query:
GROUP BY is useful when comparing results across departments, classes, product categories, or sales regions.
What if the school only wants to display classes with an average score of 75 or higher?
SQL provides the HAVING keyword to filter grouped results.
SELECT
Class,
AVG(Score) AS AverageScore
FROM Students
GROUP BY Class
HAVING AVG(Score) >= 75;
This query:
WHERE and HAVING both filter information, but they perform different jobs.
Example using WHERE:
SELECT *
FROM Students
WHERE Score >= 70;
Example using HAVING:
SELECT Class, AVG(Score) AS AverageScore
FROM Students
GROUP BY Class
HAVING AVG(Score) >= 75;
Remember: WHERE filters rows, while HAVING filters grouped results.
You can combine several SQL functions to answer different questions about your database.
SELECT COUNT(*) AS TotalStudents
FROM Students;
SELECT SUM(Score) AS TotalScore
FROM Students;
SELECT AVG(Score) AS AverageScore
FROM Students;
SELECT
MAX(Score) AS HighestScore,
MIN(Score) AS LowestScore
FROM Students;
SELECT
Class,
COUNT(*) AS NumberOfStudents
FROM Students
GROUP BY Class;
SELECT
Category,
AVG(Price) AS AveragePrice
FROM Products
GROUP BY Category;
Assume you have a database table called Students with the following columns:
Write SQL queries to complete these tasks:
In this module, you have learned how SQL functions help you calculate and understand information stored in a database.
A school wants to prepare a simple performance report for its students. Write SQL queries that can:
In Module 7, you will learn about combining and managing query results. You will explore how to combine information from different queries and work with database results more effectively.
CRMNuggets โ Learn. Practice. Get Certified.