MySQL provides several aggregate functions that allow you to perform calculations on sets of data within a table. These functions are often used in conjunction with the GROUP BY clause to operate on groups of rows.
Here’s a breakdown of some commonly used aggregate functions:
1. COUNT()
The COUNT() function is used to count the number of rows in a set, satisfying a given condition.
Syntax:
SELECT COUNT(student_id) FROM students;This query gives the total count of student in the student table.
2. SUM()
The SUM() function calculates the sum of values in a numeric column.
Syntax:
SELECT SUM(grade) FROM students;This query gives the total grade of all student in the student table.
3. AVG()
The AVG() function calculates the average of values in a numeric column.
Syntax:
SELECT AVG(grade) FROM students;This query gives the average grade among students.
4. MIN()
The MIN() function returns the minimum value in a set of values.
Syntax:
SELECT MIN(age) FROM students;This query gives the minimum age among students.
5. MAX()
The MAX() function returns the maximum value in a set of values.
Syntax:
SELECT MAX(grade) FROM students;This query gives the maximum grade among students.