MySQL - Aggregate Function
What is an Aggregate Function?
An aggregate function is a special type of function used in databases or data analysis to summarize or combine multiple values into a single value.
Imagine you have a list of numbers—like test scores—and you want to find the average, total, highest, or lowest score. Instead of checking each score manually, you can use aggregate functions to do it quickly!
Common Aggregate Functions
Function | What it Does |
---|---|
SUM() |
Adds up all the values |
AVG() |
Calculates the average (mean) |
MAX() |
Finds the highest value |
MIN() |
Finds the lowest value |
COUNT() |
Counts how many values there are |
Example: Student Marks Table
Student Name | Subject | Marks |
---|---|---|
Ali | Math | 85 |
Sara | Math | 90 |
Raza | Math | 78 |
Zoya | Math | 92 |
Let’s use some aggregate functions on the Marks column:
-
SUM(Marks)
→ 85 + 90 + 78 + 92 = 345