An aggregate function can be used to perform calculations on a set of values, which will then return a single value. We can use an aggregate function either with the GROUP BY clause or without it.

**SUM**

SUM calculates the sum of given values.

Query to calculate the sum of all the salaries from the entire employee table:

`SELECT SUM(salary) as total_salary FROM employee;`

Query to calculate the sum of all the salaries for each department in the employee table:

SELECT dept_id, SUM(salary) as total_salary_per_dept

FROM employee

GROUP BY dept_id;

**AVG**

AVG calculates the average of given values.

Query to calculate the average of all the salaries from the entire employee table:

`SELECT AVG(salary) as avg_salary FROM employee;`

Query to calculate the average of all the salaries for each department in the employee table:

SELECT dept_id, AVG(salary) as avg_salary_per_dept

FROM employee

GROUP BY dept_id

**MIN**

Find the minimum value from the given set of values.

Query to find the minimum salary from the entire employee table:

`SELECT MIN(salary) as min_salary FROM employee;`

Query to find the Minimum salaries for each department in the employee table:

SELECT dept_id, MIN(salary) as min_salary_per_dept

FROM employee

GROUP BY dept_id

**MAX**

Find the maximum value from the given set of values.

Query to find the maximum salary from the entire employee table:

`SELECT MAX(salary) as max_salary FROM employee;`

Query to find the Maximum salaries for each department in the employee table:

SELECT dept_id, MAX(salary) as max_salary_per_dept

FROM employee

GROUP BY dept_id

**COUNT**

COUNT returns the number of records.

Query to find the total number of records from the entire employee table:

`SELECT COUNT(emp_id) as no_of_emp FROM employee;`

Query to find the total number of employees in each department from the entire employee table:

SELECT dept_id, COUNT(emp_id) as no_of_emp_per_dept

FROM employee

GROUP BY dept_id;

## 0 Comments