MySQL COUNT(), AVG() and SUM() Functions
MySQL COUNT(), AVG() and SUM() Functions
These are aggregate functions in MySQL used to perform calculations on a set of values and return a single result.
📌 1. COUNT() Function
The COUNT() function is used to count the number of rows that match a specific condition.
✅ Syntax:
Example Table: students
| id | name | age | marks |
|---|---|---|---|
| 1 | John | 20 | 85 |
| 2 | Emma | 21 | 90 |
| 3 | Raj | 22 | 76 |
| 4 | Arjun | 23 | 92 |
| 5 | Sara | 19 | NULL |
✔ Example: Count All Rows
Output:
| TotalStudents |
|---|
| 5 |
✔ Example: Count Non-NULL Values in marks
Output:
| StudentsWithMarks |
|---|
| 4 |
📌 2. AVG() Function
The AVG() function returns the average value of a numeric column.
✅ Syntax:
✔ Example: Find Average Marks
| AverageMarks |
|---|
| 85.75 |
Note: AVG() ignores NULL values.
✔ AVG() with Condition
📌 3. SUM() Function
The SUM() function returns the total sum of numeric values in a column.
✅ Syntax:
✔ Example: Total Marks
Output:
| TotalMarks |
|---|
| 343 |
(76 + 85 + 90 + 92 = 343 → NULL ignored)
✔ SUM() with Condition
🧠Combine All Functions
🎯 Summary Table
| Function | Purpose | Ignores NULL Values? |
|---|---|---|
| COUNT() | Counts rows/values | ✔ Except COUNT(*) |
| AVG() | Calculates average | ✔ Yes |
| SUM() | Adds all values | ✔ Yes |
