aggregate functions,final
DESCRIPTION
ORACLETRANSCRIPT
![Page 1: Aggregate Functions,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/1.jpg)
RELATIONAL DATABASE RELATIONAL DATABASE MANAGEMENT SYSTEMMANAGEMENT SYSTEM
PRESENTED TO:Prof. kavita Prof. kavita
SainiSaini
PRESENTED BY:MUKESH PANDEY
GRADUATE SCHOOL OF
BUSINESS&ADMN,GREATER NOIDA
![Page 2: Aggregate Functions,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/2.jpg)
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.
![Page 3: Aggregate Functions,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/3.jpg)
Aggregate functions used
Some of the commonly used aggregate functionsare :• SUM• COUNT• AVG• MIN• MAX
![Page 4: Aggregate Functions,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/4.jpg)
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,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/5.jpg)
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,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/6.jpg)
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,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/7.jpg)
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,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/8.jpg)
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,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/9.jpg)
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,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/10.jpg)
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,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/11.jpg)
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,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/12.jpg)
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,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/13.jpg)
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,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/14.jpg)
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;
![Page 15: Aggregate Functions,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/15.jpg)
Count functionCount function
The COUNT() function returns the number of rows that matches a specified criteria.
The COUNT(column_name) function returns the number of values (NULL values will not be counted) of the specified column:
SQL COUNT Syntax: SELECT COUNT(column_name) FROM
table_name
![Page 16: Aggregate Functions,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/16.jpg)
We have the following "Orders" table
O_Id OrderDate OrderPrice Customer
1 2008/11/12 1000 Hansen
2 2008/10/23 1600 Nilsen
3 2008/09/02 700 Hansen
4 2008/09/03 300 Hansen
5 2008/08/30 2000 Jensen
6 2008/10/04 100 Nilsen
![Page 17: Aggregate Functions,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/17.jpg)
Performing count
Now we want to count the number of orders from "Customer Nilsen".
We use the following command:-
SELECT COUNT(Customer) AS CustomerNilsen FROM OrdersWHERE Customer='Nilsen'
![Page 18: Aggregate Functions,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/18.jpg)
CustomerNilsen
2
The result of the SQL statement above will be 2, because the customer Nilsen has made 2 orders in total:
![Page 19: Aggregate Functions,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/19.jpg)
GROUP BY clauseGROUP BY clause
Aggregate functions often need an added GROUP BY statement.
The GROUP BY Statement The GROUP BY statement is used in conjunction with the
aggregate functions to group the result-set by one or more columns.
SQL GROUP BY Syntax SELECT column_name,
aggregate_function(column_name)FROM table_name GROUP BY column_name
![Page 20: Aggregate Functions,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/20.jpg)
Performing group by
O_Id OrderDate OrderPrice Customer1 2008/11/12 1000 Hansen2 2008/10/23 1600 Nilsen3 2008/09/02 700 Hansen4 2008/09/03 300 Hansen5 2008/08/30 2000 Jensen6 2008/10/04 100 Nilsen
We have the following "Orders" table:
SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer
Now we want to find the total sum (total order) of each customer.We will have to use the GROUP BY statement to group the customers.We use the following command:
![Page 21: Aggregate Functions,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/21.jpg)
Customer SUM(OrderPrice)
Hansen 2000
Nilsen 1700
Jensen 2000
The result-set will look like this:
![Page 22: Aggregate Functions,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/22.jpg)
Having clauseHaving clause
The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.
SQL HAVING Syntax SELECT column_name,
aggregate_function(column_name)FROM table_name GROUP BY column_nameHAVING aggregate_function(column_name)
![Page 23: Aggregate Functions,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/23.jpg)
Performing having
SELECT Customer,SUM(OrderPrice) FROM OrdersGROUP BY CustomerHAVING SUM(OrderPrice)<2000
Customer SUM(OrderPrice)Nilsen 1700
Now we want to find if any of the customers have a total order of less than 2000.We use the following command:
The result-set will look like this:
![Page 24: Aggregate Functions,Final](https://reader035.vdocuments.mx/reader035/viewer/2022081602/5560edb1d8b42a016e8b5659/html5/thumbnails/24.jpg)