aggregate functions

20
AGGREGATE FUNCTIONS

Upload: syed

Post on 11-Jan-2016

221 views

Category:

Documents


0 download

DESCRIPTION

Aggregate Functions

TRANSCRIPT

Page 1: Aggregate Functions

AGGREGATE FUNCTIONS

Page 2: Aggregate Functions

Aggregate Functions

What is an aggregate function? An aggregate function summarizes the results

of an expression over a number of rows, returning a single value. The general syntax for most of the aggregate functions is as follows: aggregate_function ([DISTINCT|ALL] expression)

Page 3: Aggregate Functions

Commonly used Aggregate functions

Some of the commonly used aggregate functionsare :• SUM• COUNT• AVG• MIN• MAX

Page 4: Aggregate Functions

Examples

Consider the following Employee table:

EMPLOYEE ( EMP_ID, NAME, DEPT_NAME, SALARY)

CREATE TABLE EMPLOYEE ( EMP_ID NUMBER, NAME VARCHAR2(50), DEPT_NAME VARCHAR2(50), SALARY NUMBER);

Page 5: Aggregate Functions

Employee Table (Contd….)

Run the following script to insert the records in the table

INSERT INTO EMPLOYEE VALUES (100,'ABC','ENG',50000);INSERT INTO EMPLOYEE VALUES (101,'DEF','ENG',60000);INSERT INTO EMPLOYEE VALUES (102,'GHI','PS',50000);INSERT INTO EMPLOYEE VALUES (103,'JKL','PS',70000);INSERT INTO EMPLOYEE VALUES (104,'MNO','SALES',75000);INSERT INTO EMPLOYEE VALUES (105,'PQR','MKTG',70000);INSERT INTO EMPLOYEE VALUES (106,‘STU','SALES',null);COMMIT;

Page 6: Aggregate Functions

Select on Employee Table

After the insert when we query the Employee table we get the following results:

Select * from Employee;

Page 7: Aggregate Functions

Performing SUM

Query 1: To find the sum of all salaries in the organization:SELECT SUM(SALARY) FROM EMPLOYEE;375000

Query 2: To find the sum of the salaries grouped by deptSELECT SUM(SALARY) FROM EMPLOYEE GROUP BYDEPT_NAME

Page 8: Aggregate Functions

SUM (Continued)

If we take a look at the previous query the information won’ttell us what’s the sum for a particular department. So to include that information we add DEPT_NAME in the SELECT

SELECT DEPT_NAME,SUM(SALARY) FROM EMPLOYEE GROUP BY DEPT_NAME;

Page 9: Aggregate Functions

SUM (Continued…..)

The query in the previous slide lists the information for all the departments. What if we want the information to be restricted only for a particular department like Engg

Is this query correct?

SELECT DEPT_NAME,SUM(SALARY) FROM EMPLOYEE GROUP BYDEPT_NAME WHERE DEPT_NAME = 'ENG';

Page 10: Aggregate Functions

SUM (Continued….)

No, the query would result in the sql error (in Oracle)ORA-00933: SQL Command not properly ended

Remember : If we use the aggregate functions then you cannot usethe WHERE clause. In order to get the result what we need to use is the HAVING clause. So the query would be

SELECT DEPT_NAME,SUM(SALARY) FROM EMPLOYEE GROUP BYDEPT_NAME HAVING DEPT_NAME = 'ENG';

Page 11: Aggregate Functions

AVG Function

Query 1: If we want to calculate the AVG of all the salaries in the organization the SQL would be

SELECT AVG(SALARY) FROM EMPLOYEE62,500

Is this what we expect????Employee table has 7 records and the salaries are 50,000+60,000+50,000+70,000+75,000+70,000+null/7 = 53571

But we obtained 62500 from the query? Why is this so????

Page 12: Aggregate Functions

AVG (Continued….)

Remember : COUNT(*) is the only function which won’t ignoreNulls. Other functions like SUM,AVG,MIN,MAX they ignore Nulls. What it means is in the previous query the salary value fora particular employee was NULL. So the query

SELECT AVG(SALARY) FROM EMPLOYEEwould ignore nulls and the way the average is calculated then wouldbe 50,000+60,000+50,000+70,000+75,000+70,000/6 = 62500

Page 13: Aggregate Functions

AVG (Continued….)

From the information given in the previous slide what do you thinkwould be the output of the following query

Select COUNT(*),COUNT(SALARY) FROM EMPLOYEE;It would be

COUNT(*) COUNT(SALARY) 7 6

Because COUNT(*) is not going to ignore the Nulls in the result whereas COUNT(SALARY) is going to ignore the Nulls.

Page 14: Aggregate Functions

AVG (Continued…..)

SELECT student_name,avg(mark) FROM student,enrolmentWHERE student.student_id=enrolment.student_id;

Which one of the following is correct for the query?(a) The query is not legal(b) The query retrieves for each student enrolled,his/her name and their average mark(c) The query retrieves for each student enrolled,his/her name and the class average mark(d) The query retrieves for each student enrolled,his/her name and the mark in each subject

Is the answer (a) or (b)??????

Page 15: Aggregate Functions

AVG (Continued….)

If option 1 is not given then the correct answer would be option 2.//Script begin

Drop table student;Drop table enrolment;create table Student(student_name varchar2(100),student_id varchar2(50));create table enrolment(student_id varchar2(50),mark number);

Page 16: Aggregate Functions

AVG (Continued….)//Script Continuedinsert into student values ('A','1');insert into student values ('B','2');insert into student values ('C','3');insert into enrolment values ('1',10);insert into enrolment values ('1',20);insert into enrolment values ('1',30);insert into enrolment values ('2',40);insert into enrolment values ('2',50);insert into enrolment values ('2',60);insert into enrolment values ('3',70);insert into enrolment values ('3',60);insert into enrolment values ('3',50);commit;

Page 17: Aggregate Functions

AVG (Continued….)

If we try to execute the query given in the question

SELECT student_name,avg(mark) FROM student,enrolmentWHERE student.student_id=enrolment.student_id;

We would get the following error in OracleORA-00937:not a single-group group function

Why is it so????

Page 18: Aggregate Functions

AVG (Continued….)

Remember : When we use any of the aggregate functions in SQL all the columns listed in the SELECT need to be part of the GROUP BY Clause. In the previous SQL

SELECT student_name,avg(mark) FROM student,enrolmentWHERE student.student_id=enrolment.student_id;

student_name, avg(mark) are the columns included in the select. avg is the aggregate function. So if we leave that one out then the column which needs to part of the group by clause would bestudent_name.

Page 19: Aggregate Functions

AVG (Final SQL)

The final SQL then would be

SELECT student_name,avg(mark)FROM student,enrolmentWHERE student.student_id=enrolment.student_idgroup by student_name;

Which would give out the desired output

Page 20: Aggregate Functions

Using MIN AND MAX

Query 1: To find the minimum salary within a particular departmentSELECT MIN(SALARY),NAME FROM EMPLOYEEGROUP BY NAME;

Query 2: To find the maximum salary within a particular departmentSELECT MAX(SALARY),NAME FROM EMPLOYEEGROUP BY NAME;