• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Aggregate Functions,Final
 

Aggregate Functions,Final

on

  • 1,266 views

ORACLE

ORACLE

Statistics

Views

Total Views
1,266
Views on SlideShare
1,205
Embed Views
61

Actions

Likes
0
Downloads
16
Comments
0

2 Embeds 61

http://www.ustudy.in 51
http://ustudy.in 10

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Aggregate Functions,Final Aggregate Functions,Final Presentation Transcript

    • RELATIONAL DATABASE MANAGEMENT SYSTEM PRESENTED TO: Prof. kavita Saini PRESENTED BY: MUKESH PANDEY GRADUATE SCHOOL OF BUSINESS&ADMN,GREATER NOIDA
    • 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.
    • Aggregate functions used
      • Some of the commonly used aggregate functions
      • are :
      • SUM
      • COUNT
      • AVG
      • MIN
      • MAX
    • 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 );
    • 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;
    • Select on Employee Table After the insert when we query the Employee table we get the following results: Select * from Employee;
    • 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
    • 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;
    • 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';
    • 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';
    • 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?
    • 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
    • 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.
    • 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;
    • Count 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
    • 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
    • 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'
    • The result of the SQL statement above will be 2, because the customer Nilsen has made 2 orders in total: CustomerNilsen 2
    • GROUP 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
    • 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
    • The result-set will look like this: Customer SUM(OrderPrice) Hansen 2000 Nilsen 1700 Jensen 2000
    • Having 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)
    • 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
    •