Group Functions in MySQL

Query to get department, total salary with respect to a department from employee table.

select DEPARTMENT, sum(SALARY) Total_Salary 
from employee 
group by department

Query to get department, total salary with respect to a department from employee table order by total salary descending.

select DEPARTMENT, sum(SALARY) Total_Salary 
from employee 
group by department 
order by Total_Salary desc

Query to get department, number of employees in a department, total salary with respect to a department from employee table order by total salary descending.

select DEPARTMENT, count(FIRST_NAME), 
sum(SALARY) Total_Salary 
from employee 
group by department 
order by Total_Salary desc

Query to get department wise average salary from employee table order by salary ascending.

select DEPARTMENT, avg(SALARY) AvgSalary 
from employee 
group by department 
order by AvgSalary asc

Query to get department wise maximum salary from employee table order by salary ascending.

select DEPARTMENT, max(SALARY) MaxSalary
from employee
group by department
order by MaxSalary asc

Query to get department wise minimum salary from employee table order by salary ascending.

select DEPARTMENT, min(SALARY) MinSalary
from employee
group by department
order by MinSalary asc

Query to select number of employees joined with respect to year and month from employee table.

select year(JOINING_DATE) Join_Year, 
month(JOINING_DATE) Join_Month, 
count(*) Total_Emp 
from employee 
group by year(JOINING_DATE), month(JOINING_DATE)

Query to select department, total salary with respect to a department from employee table where total salary greater than 800000 order by Total_Salary descending.

select department, sum(SALARY) Total_Salary 
from employee
group by department
having sum(SALARY) > 800000 
order by Total_Salary desc

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *