05. Aggregate Functions
05. Aggregate Functions
SQL Aggregate functions are functions where the values of multiple rows are grouped as input on
certain criteria to form a single value result of more significant meaning.
It is used to summarize data, by combining multiple values to form a single result.
SQL Aggregate functions are mostly used with the GROUP BY clause of the SELECT statement.
Aggregate function operates on non-NULL values only (except COUNT).
Count():
Count(*): Returns the total number of records .i.e 6.
Count(salary): Return the number of Non-Null values over the column salary. i.e 5.
Count(Distinct Salary): Return the number of distinct Non-Null values over the column salary .i.e
5.
Sum():
sum(salary): Sum all Non-Null values of Column salary i.e., 310
sum(Distinct salary): Sum of all distinct Non-Null values i.e., 250.
Avg():
Avg(salary) = Sum(salary) / count(salary) = 310/5
Avg(Distinct salary) = sum(Distinct salary) / Count(Distinct Salary) = 250/4
Min():
Min(salary): Minimum value in the salary column except NULL i.e., 40.
Max():
Max(salary): Maximum value in the salary i.e., 80.
Queries
--Count the number of employees
SELECT COUNT(*) AS TotalEmployees FROM Employee;