AGGREGATE FUNCTIONS Prof. Sin-Min Lee Surya Bhagvat CS 157A – Fall 2005.

Slides:



Advertisements
Similar presentations
Copyright  Oracle Corporation, All rights reserved. 4 Aggregating Data Using Group Functions.
Advertisements

Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs WHERE SUM (duration) = 100; (this will give an error)
Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: Identify the available group.
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
Copyright  Oracle Corporation, All rights reserved. 5 Aggregating Data Using Group Functions.
1Eyad alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
GROUP FUNCTIONS. Objectives After completing this lesson, you should be able to do the following: Identify the available group functions Describe the.
5 Copyright © 2007, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
Chapter 11 Group Functions
LECTURE 10.  Group functions operate on sets of rows to give one result per group.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Enhancements to the GROUP BY Clause Fresher Learning Program January, 2012.
Chapter 3 Single-Table Queries
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-column subqueries, Multiple-row Subqueries, Correlated Subqueries 11/2/10,
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
DATABASE TRANSACTION. Transaction It is a logical unit of work that must succeed or fail in its entirety. A transaction is an atomic operation which may.
Copyright س Oracle Corporation, All rights reserved. 5 Aggregating Data Using Group Functions.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Select your database – Your database name is.
SQL-5 (Group By.. Having). Group By  Need: To apply the aggregate functions to subgroups of tuples in a relation, where the subgroups are based on some.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.
1 Querying a Single Table Structured Query Language (SQL) - Part II.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
SQL Aggregation Oracle and ANSI Standard SQL Lecture 9.
© Jalal Kawash Database Queries Peeking into Computer Science.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: –Identify the available group.
Agenda for Class - 03/04/2014 Answer questions about HW#5 and HW#6 Review query syntax. Discuss group functions and summary output with the GROUP BY statement.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-row Subqueries, Correlated Subqueries.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.
In this session, you will learn to:
Aggregating Data Using Group Functions
SQL AGGREGATE FUNCTIONS
Chapter 3 Introduction to SQL(3)
SQL in Oracle.
Group Functions Lab 6.
Working with Tables: Join, Functions and Grouping
(SQL) Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
GROUP BY & Subset Data Analysis
Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL
SQL – Entire Select.
Chapter 4 Summary Query.
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
SQL Subquery.
CS122 Using Relational Databases and SQL
Reporting Aggregated Data Using the Group Functions
Query Functions.
Reporting Aggregated Data Using the Group Functions
Aggregate Functions.
Reporting Aggregated Data Using the Group Functions
Aggregate functions Objective- understand and able to use aggregate functions in select statement Aggregate functions are used to implement calculation.
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Chapter Name SQL: Data Manipulation
Aggregating Data Using Group Functions
Group Operations Part IV.
Presentation transcript:

AGGREGATE FUNCTIONS Prof. Sin-Min Lee Surya Bhagvat CS 157A – Fall 2005

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)

Commonly used Aggregate functions 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; 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 = But we obtained 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.

AVG (Continued…..) SELECT student_name,avg(mark) FROM student,enrolment WHERE 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)??????

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);

AVG (Continued….) //Script Continued insert 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;

AVG (Continued….) If we try to execute the query given in the question SELECT student_name,avg(mark) FROM student,enrolment WHERE student.student_id=enrolment.student_id; We would get the following error in Oracle ORA-00937:not a single-group group function Why is it so????

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,enrolment WHERE 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 be student_name.

AVG (Final SQL) The final SQL then would be SELECT student_name,avg(mark) FROM student,enrolment WHERE student.student_id=enrolment.student_id group by student_name; Which would give out the desired output

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;