MySQL COUNT(), AVG(), and SUM() Functions
The COUNT(), AVG(), and SUM() functions are aggregate functions in MySQL used to perform calculations on sets of rows and return a single result. Here are examples of how they work:
1. COUNT()
Counts the number of rows in a specified table or query result.
Example:
SELECT COUNT(*) AS total_students FROM students;
This query will return the total number of students in the “students” table.
2. AVG()
Calculates the average value of a numeric column in a specified table or query result.
Example:
SELECT AVG(score) AS average_score FROM exam_results;
This query will return the average score of all students in the “exam_results” table.
3. SUM()
Calculates the sum of values in a numeric column in a specified table or query result.
Example:
SELECT SUM(quantity) AS total_quantity FROM orders;
This query will return the total quantity of all orders in the “orders” table.
These aggregate functions are useful for generating summary statistics and performing calculations on large datasets in MySQL.