SQL has many built-in functions to perform a calculation in SQL. In this tutorial, we will learn how to use COUNT() , AVG() and SUM() functions using SQL.
COUNT() function in SQL returns the number of rows that match the given criteria in a statement.
SELECT COUNT(columnName)
FROM tableName
WHERE [CONDITION];
AVG() function returns the average value of a numeric column.
SELECT AVG(columnName)
FROM tableName
WHERE [CONDITION];
SUM() function returns the total sum of a given numeric column.
SELECT SUM(columnName)
FROM tableName
WHERE [CONDITION];
Let us consider this table "Employee" for records.
Table Name: Employee
| ID | EmpName | City | Country | Gender | Salary |
| 1 | Shankar | Delhi | India | male | 25000 |
| 2 | Sourabh | Delhi | India | male | 30000 |
| 3 | Ranvijay | Mumbai | India | male | 15000 |
| 4 | Kapil | Noida | India | male | 25000 |
| 5 | Shalini | Jaipur | India | female | 18000 |
| 6 | Rakesh | Faridabad | India | male | 23000 |
| 7 | Akshay | Mumbai | India | male | 21000 |
| 8 | Sarah | New York | US | female | 76000 |
| 9 | Rocky | Noida | India | male | 28000 |
For getting the total number of an employee from the table "Employee", a query will be:
SELECT COUNT(EmpName) as TotalEmployee
FROM Employee;
| TotalEmployee |
| 9 |
For getting the average salary of an employee from table "Employee", a query will be:
SELECT AVG(Salary) as AvgSalary
FROM Employee;
| AvgSalary |
| 29000 |
For getting the total salary of all employee from table "Employee", a query will be:
SELECT SUM(Salary) as TotalSalary
FROM Employee;
| TotalSalary |
| 261000 |