Upcoming SlideShare
×

# Aggregate Functions,Final

1,285

Published on

ORACLE

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
1,285
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
24
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Aggregate Functions,Final

1. 1. RELATIONAL DATABASE MANAGEMENT SYSTEM PRESENTED TO: Prof. kavita Saini PRESENTED BY: MUKESH PANDEY GRADUATE SCHOOL OF BUSINESS&ADMN,GREATER NOIDA
2. 2. Aggregate Functions <ul><li>What is an aggregate function? </li></ul><ul><li>An aggregate function summarizes the results of an expression over a number of rows, returning a single value. </li></ul>
3. 3. Aggregate functions used <ul><li>Some of the commonly used aggregate functions </li></ul><ul><li>are : </li></ul><ul><li>SUM </li></ul><ul><li>COUNT </li></ul><ul><li>AVG </li></ul><ul><li>MIN </li></ul><ul><li>MAX </li></ul>
4. 4. 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 );
5. 5. 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;
6. 6. Select on Employee Table After the insert when we query the Employee table we get the following results: Select * from Employee;
7. 7. 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 dept SELECT SUM(SALARY) FROM EMPLOYEE GROUP BY DEPT_NAME
8. 8. SUM (Continued) If we take a look at the previous query the information won’t tell 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;
9. 9. 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 BY DEPT_NAME WHERE DEPT_NAME = 'ENG';
10. 10. 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 use the 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 BY DEPT_NAME HAVING DEPT_NAME = 'ENG';
11. 11. 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 EMPLOYEE 62,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?
12. 12. AVG (Continued….) Remember : COUNT(*) is the only function which won’t ignore Nulls. Other functions like SUM,AVG,MIN,MAX they ignore Nulls. What it means is in the previous query the salary value for a particular employee was NULL. So the query SELECT AVG(SALARY) FROM EMPLOYEE would ignore nulls and the way the average is calculated then would be 50,000+60,000+50,000+70,000+75,000+70,000/6 = 62500
13. 13. AVG (Continued….) From the information given in the previous slide what do you think would 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.
14. 14. Using MIN AND MAX Query 1: To find the minimum salary within a particular department SELECT MIN(SALARY),NAME FROM EMPLOYEE GROUP BY NAME; Query 2: To find the maximum salary within a particular department SELECT MAX(SALARY),NAME FROM EMPLOYEE GROUP BY NAME;
15. 15. Count function <ul><li>The COUNT() function returns the number of rows that matches a specified criteria. </li></ul><ul><li>The COUNT(column_name) function returns the number of values (NULL values will not be counted) of the specified column: </li></ul><ul><li>SQL COUNT Syntax: </li></ul><ul><li>SELECT COUNT(column_name) FROM table_name </li></ul>
16. 16. We have the following &quot;Orders&quot; 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
17. 17. Performing count <ul><li>Now we want to count the number of orders from &quot;Customer Nilsen&quot;. </li></ul><ul><li>We use the following command:- </li></ul><ul><li>SELECT COUNT(Customer) AS CustomerNilsen FROM OrdersWHERE Customer='Nilsen' </li></ul>
18. 18. The result of the SQL statement above will be 2, because the customer Nilsen has made 2 orders in total: CustomerNilsen 2
19. 19. GROUP BY clause <ul><li>Aggregate functions often need an added GROUP BY statement. </li></ul><ul><li>The GROUP BY Statement </li></ul><ul><li>The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns. </li></ul><ul><li>SQL GROUP BY Syntax </li></ul><ul><li>SELECT column_name, aggregate_function(column_name)FROM table_name GROUP BY column_name </li></ul>
20. 20. Performing group by We have the following &quot;Orders&quot; table: 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: 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 SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer
21. 21. The result-set will look like this: Customer SUM(OrderPrice) Hansen 2000 Nilsen 1700 Jensen 2000
22. 22. Having clause <ul><li>The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions. </li></ul><ul><li>SQL HAVING Syntax </li></ul><ul><li>SELECT column_name, aggregate_function(column_name)FROM table_name GROUP BY column_nameHAVING aggregate_function(column_name) </li></ul>
23. 23. Performing having 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: SELECT Customer,SUM(OrderPrice) FROM OrdersGROUP BY CustomerHAVING SUM(OrderPrice)<2000 Customer SUM(OrderPrice) Nilsen 1700
1. #### A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.