Aggregate functions are indispensable when analyzing data in MySQL. These functions perform specific calculations on a dataset, providing valuable information. In this section, we will explain the types of aggregate functions commonly used and how to use them.
▼For information on the tables used in this article, please refer to this link.
▼youtube
Using the MAX Function
The MAX function returns the highest value in a specified column. For example, to find the highest score in the entry_exam column of the students table, you would use the following query:
SELECT
MAX(entry_exam)
FROM students;
Using the MIN Function
The MIN function returns the smallest value in a column. To find the lowest score in the students table, you would use this query:
SELECT
MIN(entry_exam)
FROM students;
Calculating Total Value with the SUM Function
The SUM function calculates the total of all values in a column. To find the total of scores in the entry_exam column, you would use:
SELECT
SUM(entry_exam)
FROM students;
Calculating Average Value with the AVG Function
The AVG function returns the average value of a column. To find the average score in the entry_exam column of the students table, you can use this query:
SELECT
AVG(entry_exam)
FROM students;
Counting Data with the COUNT Function
The COUNT function counts the number of data entries in a column. For example, to find the number of records that have data in the entry_exam column, you would use:
SELECT
COUNT(entry_exam)
FROM students;
Summary
Aggregate functions are a powerful tool in data analysis. By mastering these functions, you can gain valuable insights from the information within your database. However, when using aggregate functions, it’s important to understand the nature of your data and the purpose of your analysis.