MySQL - count(),avg(),sum() functions

In MySQL, the COUNT(), AVG(), and SUM() functions are aggregate functions used to perform calculations on sets of values.  Here's a breakdown of each:

Note: they are not case-sensitive, so you can also write as count(),avg(),sum().

 1. COUNT()

  • Purpose: Returns the number of rows that match a specified condition.

  • Syntax:SELECT COUNT(column_name) FROM table_name;

  • Examples:

    • Count all rows (including NULLs):

    • SELECT COUNT(*) FROM employees;

    • Count only non-NULL values in a column:SELECT COUNT(salary) FROM employees;

2. AVG()

  • Purpose: Returns the average value of a numeric column (ignores NULLs).

  • Syntax:SELECT AVG(column_name) FROM table_name;

  • Example:SELECT AVG(salary) FROM employees;

 3. SUM()

  • Purpose: Returns the total sum of a numeric column (ignores NULLs).

  • Syntax:SELECT SUM(column_name) FROM table_name;

  • Example:SELECT SUM(salary) FROM employees;