Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Successfully reported this slideshow.

Like this presentation? Why not share!

- Aggregate functions by sinhacp 554 views
- Function and Importance of Aggregat... by HAPPHO Care 3659 views
- Agreggates i by Claudia Gomez 914 views
- Aggregating Data Using Group Funct... by Salman Memon 302 views
- Aggregate Function - Database by Shahadat153031 347 views
- Structured Query Language (SQL) - L... by Beat Signer 37824 views

No Downloads

Total views

2,284

On SlideShare

0

From Embeds

0

Number of Embeds

110

Shares

0

Downloads

67

Comments

0

Likes

3

No embeds

No notes for slide

- 1. RELATIONAL DATABASE MANAGEMENT SYSTEM PRESENTED TO: Prof. kavita Saini PRESENTED BY: MUKESH PANDEY GRADUATE SCHOOL OF BUSINESS&ADMN,GREATER NOIDA
- 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. 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. 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. 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. Select on Employee Table After the insert when we query the Employee table we get the following results: Select * from Employee;
- 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
- 17. Performing count <ul><li>Now we want to count the number of orders from "Customer Nilsen". </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. The result of the SQL statement above will be 2, because the customer Nilsen has made 2 orders in total: CustomerNilsen 2
- 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. Performing group by We have the following "Orders" 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. The result-set will look like this: Customer SUM(OrderPrice) Hansen 2000 Nilsen 1700 Jensen 2000
- 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. 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

No public clipboards found for this slide

×
### Save the most important slides with Clipping

Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.

Be the first to comment