SQL Server: Order by, Group by clauses | Sorting and Grouping in SQL Server


--Order by EmpName (ascending order by default)
select * 
from tabEmployee 
order by EmpName

--Order by EmpName Descending
select * 
from tabEmployee 
order by EmpName desc

--Order by Designation (ascending order by default)
select * 
from tabEmployee 
order by Designation

--Order by salary in descending order
select * 
from tabEmployee 
order by Salary desc

--Select the total salary of all employees of each designation
select Designation,Sum(salary) 
from tabEmployee 
group by Designation

--Select the total salary of all employees of each designation
select Designation,Sum(salary) as Total_Salary 
from tabEmployee 
group by Designation

--Select the Total salary spend on all employees of each department
select Department,Sum(salary) 
from tabEmployee 
group by Department

--Select the "Total salary" spend on all employees 
--of each department sorted by "Total salary"
select Department,Sum(salary) as Total_Salary 
from tabEmployee 
group by Department 
order by Total_Salary

--Select the Count the employees 
--of each designation on each department
select Department,Designation,count(empid) 
from tabEmployee 
group by DepartmentDesignation

--Select the Count the employees 
--of each designation on each department and sort by department
select Department,Designation,count(empid) as 'No.Of.Emps' 
from tabEmployee 
group by DepartmentDesignation 
order by Department

No comments:

Post a Comment