Wednesday, 13 December 2017

Calculate Total Salary Using Group By Clause In SQL

// Create Database
  Create  database  LP;

//Select Database
  Use  LP;

//Create table MIS
Create table MIS (Employee_ID  INT  Not NULL, Employee_Name  Varchar (20)NOT NULL, Salary INT  NOT NULL, CIBIL INT NOT NULL);
//Insert Data into MIS
INSERT into MIS(Employee_ID, Employee_Name,Salary, CIBIL) values (237998, 'Ram',89555, 985 );
INSERT into MIS (Employee_ID, Employee_Name,Salary, CIBIL) values (637908, 'Sham',90000, 895 );
INSERT into MIS (Employee_ID, Employee_Name,Salary, CIBIL) values (231798, 'Rohan',89555, 225 );
INSERT into MIS (Employee_ID, Employee_Name,Salary, CIBIL) values (352908, 'Sham',90000, 895 );
INSERT into MIS (Employee_ID, Employee_Name,Salary, CIBIL) values (685098, 'Rohan',23555, 236 );
INSERT into MIS (Employee_ID, Employee_Name,Salary, CIBIL) values (658990, 'Ram',89555, 985 );
INSERT into MIS(Employee_ID, Employee_Name,Salary, CIBIL) values (237998, 'Rajesh',20555, 985 );
INSERT into MIS (Employee_ID, Employee_Name,Salary, CIBIL) values (637908, 'Rajesh',20555, 895 );
INSERT into MIS (Employee_ID, Employee_Name,Salary, CIBIL) values (231798, 'Raj',25555, 225 );
INSERT into MIS (Employee_ID, Employee_Name,Salary, CIBIL) values (352908, 'Raj',25555, 985 );
INSERT into MIS (Employee_ID, Employee_Name,Salary, CIBIL) values (685098, 'Rohan',23555, 236 );
INSERT into MIS (Employee_ID, Employee_Name,Salary, CIBIL) values (658990, 'Ram',89555, 985 );

 //Retrieve  Data from MIS
      Select * from MIS;

// Calculate Total Salary of all employee
   Select SUM (Salary) as "Total Salary" from MIS;   //Where "Total Salary" is new Column Name

// Calculate Total Salary Group BY Employee Name
Select  Employee_Name,SUM (Salary) as "Total Salary" from  MIS Group BY  Employee_Name;

// Calculate Total Salary Group BY  Employee Id
Select  Employee_ID,SUM (Salary) as "Total Salary" from MIS  Group BY   Employee_ID;

//Retrieve data from MIS
  Select * from MIS;

//Calculate Total Salary Where Salary <40000
Select  SUM ( Salary) AS "Total Salary" from MIS where Salary <40000;

//Calculate Total Salary using Distinct(it will take single value from duplicate value)Where Salary <40000

Select  SUM (Distinct Salary) AS "Total Salary" from MIS where Salary <40000;

//Retrieve data from MIS

 Select * from MIS;

No comments:

Post a Comment

Global Infrastructure and its components in AWS

You may operate your workloads wherever and however you choose with the AWS Global Infrastructure, and you'll use the same network, cont...