Aggregate Functions Presenter: Sushma Bandekar CS 157(A) – Fall 2006.

Slides:



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

 Database is SQL1.mdb ◦ import using MySQL Migration Toolkit 
CSC271 Database Systems Lecture # 11.
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
Data Warehousing/Mining 1 Data Warehousing/Mining Comp 150 Aggregation in SQL (not in book) Instructor: Dan Hebert.
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.
Said Salomon Unitrin Direct Insurance T-SQL Aggregate Functions Said Salomon.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 6: Set Functions.
Introduction to Oracle9i: SQL1 SQL Group Functions.
SQL Neyha Amar CS 157A, Fall Inserting The insert statement is used to add a row of data into a table Strings should be enclosed in single quotes,
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Computer Science 101 Web Access to Databases SQL – Extended Form.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
Chapter 3 Single-Table Queries
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
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.
Using Special Operators (LIKE and IN)
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
Lecture7:Data Manipulation in SQL Advanced Queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture7 1.
SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
1 SQL-3 Tarek El-Shishtawy Professor Ass. Of Computer Engineering.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
Comp12 cont…. Using Quotes Note that we have used single quotes around the conditional values in the examples. SQL uses single quotes around text values.
Copyright © Curt Hill Queries in SQL More options.
IST 210 SQL Todd Bacastow IST 210: Organization of Data.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
SQL Aggregation Oracle and ANSI Standard SQL Lecture 9.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
Course title: Database-ii Chap No: 03 “Advanced SQL” Course instructor: ILTAF MEHDI.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
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.
Aggregating Data Using Group Functions
SQL AGGREGATE FUNCTIONS
Chapter 3 Introduction to SQL(3)
CS311 Database Management system
Chapter 5: Aggregate Functions and Grouping of Data
Aggregating Data Using Group Functions
SQL : Query Language Part II CS3431.
(SQL) Aggregating Data Using Group Functions
CS 405G: Introduction to Database Systems
Aggregating Data Using Group Functions
Session 3 Welcome: To session 3-the sixth learning sequence
SQL – Entire Select.
Chapter 4 Summary Query.
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
SQL Aggregation.
CS122 Using Relational Databases and SQL
Reporting Aggregated Data Using the Group Functions
Query Functions.
Section 4 - Sorting/Functions
Joins and other advanced Queries
Reporting Aggregated Data Using the Group Functions
Set Operations Union Intersect Minus.
Aggregate Functions.
Reporting Aggregated Data Using the Group Functions
分组函数 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 Presenter: Sushma Bandekar CS 157(A) – Fall 2006

Aggregate Function: Aggregate functions are functions that take a collection of values as input and return a single value.  Behavior of Aggregate Functions: Operates - on a single column Return - a single value. Used only in the SELECT list and in the HAVING clause.

Behavior of Aggregate function Continued… Accepts: DISTINCT : consider only distinct values of the argument expression. ALL : consider all values including all duplicates. Example: SELECT COUNT( DISTINCT column_name)

Types of SQL Aggregate Functions SUM SUM AVG AVG MIN MIN MAX MAX COUNT COUNT

Input to Aggregate Function SUM and AVG : SUM and AVG :  Operates only on collections of numbers. MIN, MAX and COUNT MIN, MAX and COUNT  Operates on collection of numeric and non-numeric data types. data types.  Each function eliminates NULL values and operates on Non- null values. Non- null values.

Staff snofnamelnamesalaryposition SL100JohnWhite Manager SL101SusanBrand Manager SL102DavidFord Project Manager SL103AnnBeech SL104MaryHowe

SUM() Returns: The sum of the values in a specified column. Example: Find the total/sum of the Managers salary Query: SELECT SUM( salary) AS sum_salary FROM Staff WHERE Staff.position = ‘Manager’; Result: sum_salary

AVG() Returns: The average of the values in a specified column. Example: Find the average of the Project Managers salary. Query: SELECT AVG( DISTINCT salary) AS avg_salary FROM Staff WHERE Staff.position = ‘Project Manager’; Result: avg_salary // Error in Result // avg_salary = // What is wrong?

Revised Query for AVG() Query: SELECT AVG(ALL salary) AS avg_salary FROM Staff WHERE Staff.position = ‘Project Manager’; Result : avg_salary CAUTION: Using DISTINCT and ALL in SUM() and AVG()

Staff snofnamelnamesalaryposition SL100JohnWhite Manager SL101SusanBrand Manager SL102DavidFord Project Manager SL103AnnBeech SL104MaryHowe

MIN() and MAX() Returns: MIN() returns the smallest value of a column. MAX() returns the largest value of a column. Example: Find the minimum and maximum staff salary. Query: SELECT MIN( salary) AS min_salary, MAX (salary) AS max_salary FROM Staff; Result: min_salary max_salary

COUNT() Returns: The number of values in the specified column. Example: Count number of staffs who are Manager. Query: SELECT COUNT(sno) AS sno_count FROM Staff WHERE Staff.position = ‘Manager’; Result: sno_count 2

Use of COUNT() and SUM() Example: Find the total number of Managers and the sum of there salary. Query: SELECT COUNT( sno) AS sno_count, SUM(salary) AS sum_salary From Staff WHERE Staff.position = ‘Manager’; snofnamelnamesalaryposition SL100JohnWhite Manager SL101SusanBrand Manager SUM COUNT

COUNT() and SUM() continued Result: Result: sno_countsum_salary

Staff snofnamelnamesalaryposition SL100JohnWhite Manager SL101SusanBrand Manager SL102DavidFord Project Manager SL103AnnBeech SL104MaryHowe

COUNT(*) Input: There is no input to this function. Returns: It counts all the rows of a table, regardless of whether Nulls or the duplicate occur. Example: How many Project Manager salary is more than Query: SELECT COUNT(*) AS Count_Salary FROM Staff WHERE Staff.position = ‘Project Manager’ AND Staff.salary >

COUNT(*) continued… Result: Result: Count_ Salary 2

Usage of Aggregation Functions Use of GROUP BY Use of GROUP BY Use of HAVING Use of HAVING

Use of GROUP BY GROUP BY: It groups the data from the SELECT table and produce a single summary row for each group When Using GROUP BY: 1.Each item in the SELECT list must be single valued per group. 2.SELECT clause may only contain:  Columns names  Aggregate function  Constants  An expression involving combinations of the above. 3. All column names in SELECT list must appear in the GROUP BY clause unless the name is used only in the aggregate function.

Staff snobnofnamelnamesalaryposition SL100B3JohnWhite Manager SL101B5SusanBrand Manager SL102B3DavidFord Project Manager SL103B5AnnBeech SL104B7MaryHowe

GROUP BY Example: Find the number of staff working in each branch and the sum of their salaries. Query: SELECT bno, COUNT(sno) AS count, SUM(salary) AS sum FROM Staff GROUP BY bno ORDER by bno; Result: bnocountsum B B B

SQL’S ROLE bnosnosalaryCOUNT(sno)SUM(salary) B3B3SL100SL B5B5SL101SL B7Sl

USE OF HAVING HAVING clause: It is designed to be used with GROUP BY so that it can restrict the groups that appear in the final result table. Example: For each branch office with more than one member of staff, find the number of staff working in each branch and the sum of their salaries. Query: SELECT bno, COUNT(sno) AS count, SUM(salary) AS sum FROM Staff GROUP BY bno HAVING COUNT(sno) > 1 ORDER by bno;

Having Clause continued…. Result table after performing GROUP BY bno clause. Result table after performing GROUP BY bno clause. Final result table after performing Final result table after performing HAVING COUNT(sno) > 1 ORDER by bno; bnoCOUNT(sno)SUM(salary) B B B bnocountsumB B

References Database Systems : A practical approach to Design, Implementation, and Management by Database Systems : A practical approach to Design, Implementation, and Management by Thomas Connolly and Carolyn Begg. Thomas Connolly and Carolyn Begg. Database System Concepts by Silbaerschatz, Korth and Sudarshan. Database System Concepts by Silbaerschatz, Korth and Sudarshan. Database Modeling and Design by Toby J. Teorey. Database Modeling and Design by Toby J. Teorey. n n