Sunday, 10 July 2016

SQL SERVER -- Aggregate Functions-- Part-18

SQL SERVER -- Aggregate Functions-- Part-18

                   By Using Aggregate Functions we can perform following operations Like SUM, MIN, MAX, AVG and COUNT.

SUM:- Used to get Total Salary of all the employees in a EMP Table.
MAX:- Used to get Maximum Salary of all the employees in a EMP Table.
MIN:- Used to get Minimum Salary of all the employees in a EMP Table.
AVG:- Used to get Average Salary of all the employees in a EMP Table.
COUNT:- Used to get Number of Employees in a EMP Table.

Sample Table Script :-
USE tempdb
GO
CREATE TABLE EMP
(
       EMPID INT
      ,EMPNAME VARCHAR(50)
      ,SAL MONEY
      ,DOJ DATE
      ,LOC VARCHAR(50)
      ,GENDER CHAR(6)
      ,MagrID INT
      ,DEPTNO INT
)

INSERT INTO EMP(EMPID, EMPNAME, SAL, DOJ, LOC, GENDER, MagrID, DEPTNO)
VALUES(1,'Santhosh',35000,'01-12-2014','Bangalore','M',3,101)

INSERT INTO EMP
VALUES(2,'Kumari',30000,'11-05-2013','Chennai','F',3,104)

INSERT INTO EMP(EMPID, EMPNAME, SAL, DOJ, LOC, GENDER, MagrID, DEPTNO)
VALUES(3,'Kamesh',75000,'01-12-2012','Mumbai','M',NULL,105)

INSERT INTO EMP(EMPID, EMPNAME, SAL, LOC, GENDER, MagrID, DEPTNO)
VALUES(4,'Arun',44000,'Hyderabad','m',5,103)

INSERT INTO EMP(EMPID, EMPNAME, DOJ, LOC, GENDER, MagrID, DEPTNO)
VALUES(5,'Aishwarya','01-12-2014','Bangalore','f',3,103)


-- Sample Table
SELECT * FROM EMP
-- SUM
SELECT SUM(SAL) AS Total_Salary FROM EMP
-- MAX
SELECT MAX(SAL) AS Total_Salary FROM EMP
-- MIN
SELECT MIN(SAL) AS Total_Salary FROM EMP
-- AVG
SELECT AVG(SAL) AS Total_Salary FROM EMP
-- COUNT
SELECT COUNT(SAL) AS Total_Salary FROM EMP








No comments:

Post a Comment