Bordoloi and Bock Chapter 5 : Aggregate Row Functions.

Slides:



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

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.
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.
Bordoloi and Bock Chapter 3 :Single Table Query Basics.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-1 COS 346 Day 15.
Bordoloi and BockCopyright 2004 Prentice Hall, Inc.7-1 COS 346 Day 16.
Prentice Hall © COS 346 Day Agenda Questions?Questions? Assignment 5 not CorrectedAssignment 5 not Corrected –2 MIA’s Assignment 6 PostedAssignment.
Prentice Hall © COS 346 Day 13. Prentice Hall © Agenda Questions?Questions? Assignment 5 PostedAssignment 5 Posted –Due March 20 (right After.
Introduction to Oracle9i: SQL1 SQL Group Functions.
Structured Query Language Part I Chapter Three CIS 218.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Microsoft Access 2010 Chapter 7 Using SQL.
Database Programming Sections 5– GROUP BY, HAVING clauses, Rollup & Cube Operations, Grouping Set, Set Operations 11/2/10.
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
SQL Unit 5 Aggregation, GROUP BY, and HAVING Kirk Scott 1.
Bordoloi and Bock Chapter 4 : Adding Power to Queries.
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)
Concepts of Database Management Seventh Edition
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.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Database Applications – Microsoft Access Lesson 4 Working with Queries 36 Slides in Presentation.
Basic Group Functions (without GROUP BY clause) Week 5 – Chapter 5.
1 Querying a Single Table Structured Query Language (SQL) - Part II.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Session 9 Accessing Data from a Database. RDBMS and Data Management/ Session 9/2 of 34 Session Objectives Describe the SELECT statement, its syntax and.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
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.
CS580 Advanced Database Topics
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Aggregating Data Using Group Functions
Enhanced Guide to Oracle 10g
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
Chapter 4: Adding Power to Queries
Chapter 5: Aggregate Functions and Grouping of Data
Aggregating Data Using Group Functions
(SQL) Aggregating Data Using Group Functions
Using the Set Operators
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Aggregating Data Using Group Functions
Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL
Chapter 4 Summary Query.
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Reporting Aggregated Data Using the Group Functions
Section 4 - Sorting/Functions
Reporting Aggregated Data Using the Group Functions
Using Subqueries to Solve Queries
Reporting Aggregated Data Using the Group Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Aggregating Data Using Group Functions
Group Operations Part IV.
Presentation transcript:

Bordoloi and Bock Chapter 5 : Aggregate Row Functions.

Bordoloi and Bock AGGREGATE ROW FUCTIONS Aggregate Row functions give the user the ability to answer business questions such as:Aggregate Row functions give the user the ability to answer business questions such as:  What is the average salary of an employee in the company?  What were the total salaries for a particular year?  What are the maximum and minimum salaries in the Computer’s Department?

Bordoloi and Bock AGGREGATE ROW FUCTIONS Aggregate functions perform a variety of actions such as counting all the rows in a table, averaging a column's data, and summing numeric data.Aggregate functions perform a variety of actions such as counting all the rows in a table, averaging a column's data, and summing numeric data. Aggregates can also search a table to find the highest "MAX" or lowest "MIN" values in a column.Aggregates can also search a table to find the highest "MAX" or lowest "MIN" values in a column.

Bordoloi and Bock AGGREGATE ROW FUCTIONS List of aggregate functions including their syntax and use.List of aggregate functions including their syntax and use. Function SyntaxFunction Use SUM( [ALL | DISTINCT] expression ) The total of the (distinct) values in a numeric column/expression. AVG( [ALL | DISTINCT] expression ) The average of the (distinct) values in a numeric column/expression. COUNT( [ALL | DISTINCT] expression ) The number of (distinct) non-NULL values in a column/expression. COUNT(*) The number of selected rows. MAX(expression) The highest value in a column/expression. MIN(expression)The lowest value in a column/expression.

Bordoloi and Bock AGGREGATE ROW FUCTIONS There are two rules that you must understand and follow when using aggregates: There are two rules that you must understand and follow when using aggregates: Aggregate functions can be used in both the SELECT and HAVING clauses (the HAVING clause is covered later in this chapter). Aggregate functions can be used in both the SELECT and HAVING clauses (the HAVING clause is covered later in this chapter). Aggregate functions cannot be used in a WHERE clause.Aggregate functions cannot be used in a WHERE clause.

Bordoloi and Bock EXAMPLE The following query is wrong and will produce the Oracle ORA group function is not allowed here error message.The following query is wrong and will produce the Oracle ORA group function is not allowed here error message. SELECT * FROM employee WHERE emp_salary > AVG(emp_salary); ERROR at line 3: ORA-00934: group function is not allowed here.

Bordoloi and Bock COUNT( ) If a manager needs know how many employees work in the organization, COUNT(*) can be used to produce this information.If a manager needs know how many employees work in the organization, COUNT(*) can be used to produce this information. The COUNT(*) function counts all rows in a table.The COUNT(*) function counts all rows in a table. The wild card asterisk (*) would be used as the parameter in the function.The wild card asterisk (*) would be used as the parameter in the function. SELECT COUNT(*) FROM employee; COUNT(*) COUNT(*)

Bordoloi and Bock COUNT( ) The result table for the COUNT(*) function is a single scalar value.The result table for the COUNT(*) function is a single scalar value. Notice that the result table has a column heading that corresponds to the name of the aggregate function specified in the SELECT clause.Notice that the result table has a column heading that corresponds to the name of the aggregate function specified in the SELECT clause. The output column can be assigned a more meaningful column name as is shown in the revised query.The output column can be assigned a more meaningful column name as is shown in the revised query.

Bordoloi and Bock COUNT( ) This is accomplished by simply listing the desired column name inside double-quotes after the aggregate function specification.This is accomplished by simply listing the desired column name inside double-quotes after the aggregate function specification. SELECT COUNT(*) "Number of Employees" FROM employee; Number of Employees

Bordoloi and Bock COUNT( ) COUNT(*) is used to count all the rows in a table.COUNT(*) is used to count all the rows in a table. COUNT(column name) does almost the same thing. The difference is that you may define a specific column to be counted.COUNT(column name) does almost the same thing. The difference is that you may define a specific column to be counted. When column name is specified in the COUNT function, rows containing a NULL value in the specified column are omitted.When column name is specified in the COUNT function, rows containing a NULL value in the specified column are omitted. A NULL value stands for “unknown” or “unknowable” and must not be confused with a blank or zero.A NULL value stands for “unknown” or “unknowable” and must not be confused with a blank or zero.

Bordoloi and Bock COUNT ( ) SELECT COUNT(emp_superssn) "Number Supervised Employees" FROM employee; Number Supervised Employees In contrast the count(*) will count each row regardless of NULL values.In contrast the count(*) will count each row regardless of NULL values. SELECT COUNT(*) "Number of Employees “ FROM employee; Number of Employees

Bordoloi and Bock Using the AVG Function AVG function is used to compute the average value for the emp_salary column in the employee table.AVG function is used to compute the average value for the emp_salary column in the employee table. For example, the following query returns the average of the employee salaries.For example, the following query returns the average of the employee salaries. SELECT AVG(emp_salary) "Average Employee Salary" FROM employee; Average Employee Salary $35,500 $35,500

Bordoloi and Bock More Examples What is the average salary offered to employees?What is the average salary offered to employees? This question asks you to incorporate the concept of computing the average of the distinct salaries paid by the organization.This question asks you to incorporate the concept of computing the average of the distinct salaries paid by the organization. The same query with the DISTINCT keyword in the aggregate function returns a different average.The same query with the DISTINCT keyword in the aggregate function returns a different average. SELECT AVG(DISTINCT emp_salary) "Average Employee Salary" FROM employee; Average Employee Salary $38,200 $38,200

Bordoloi and Bock Using the SUM Function The SUM function can compute the total of a specified table column.The SUM function can compute the total of a specified table column. The SELECT statement shown here will return the total of the emp_salary column from the employee table.The SELECT statement shown here will return the total of the emp_salary column from the employee table. SELECT SUM(emp_salary) "Total Salary" FROM employee; Total Salary $284,000 $284,000

Bordoloi and Bock More Examples If management is preparing a budget for various departments, you may be asked to write a query to compute the total salary for different departments.If management is preparing a budget for various departments, you may be asked to write a query to compute the total salary for different departments. The query shown here will compute the total emp_salary for employees assigned to department #7.The query shown here will compute the total emp_salary for employees assigned to department #7. SELECT SUM(emp_salary) "Total Salary Dept 7" FROM employee WHERE emp_dpt_number = 7; Total Salary Dept $136,000 $136,000

Bordoloi and Bock MIN and MAX Functions MIN and MAX Functions The MIN function returns the lowest value stored in a data column.The MIN function returns the lowest value stored in a data column. The MAX function returns the largest value stored in a data column.The MAX function returns the largest value stored in a data column. Unlike SUM and AVG, the MIN and MAX functions work with both numeric and character data columns.Unlike SUM and AVG, the MIN and MAX functions work with both numeric and character data columns.

Bordoloi and BockExample A query that uses the MIN function to find the lowest value stored in the emp_last_name column of the employee table.A query that uses the MIN function to find the lowest value stored in the emp_last_name column of the employee table. This is analogous to determine which employee's last name comes first in the alphabet.This is analogous to determine which employee's last name comes first in the alphabet. Conversely, MAX() will return the employee row where last name comes last (highest) in the alphabet.Conversely, MAX() will return the employee row where last name comes last (highest) in the alphabet. SELECT MIN(emp_last_name), MAX(emp_last_name) FROM employee; MIN(EMP_LAST_NAME) MAX(EMP_LAST_NAME) Amin Zhu

Bordoloi and Bock Using GROUP BY with Aggregate Functions The power of aggregate functions is greater when combined with the GROUP BY clause.The power of aggregate functions is greater when combined with the GROUP BY clause. In fact, the GROUP BY clause is rarely used without an aggregate function.In fact, the GROUP BY clause is rarely used without an aggregate function. It is possible to use the GROUP BY clause without aggregates, but such a construction has very limited functionality, and could lead to a result table that is confusing or misleading.It is possible to use the GROUP BY clause without aggregates, but such a construction has very limited functionality, and could lead to a result table that is confusing or misleading.

Bordoloi and BockExample The following query displays how many employees work for each department?The following query displays how many employees work for each department? SELECT emp_dpt_number "Department", COUNT(*) "Department Count" FROM employee GROUP BY emp_dpt_number; Department Department Count

Bordoloi and Bock GROUP BY Clause Some RDBMs provides considerable flexibility in specifying the GROUP BY clause.Some RDBMs provides considerable flexibility in specifying the GROUP BY clause. The column name used in a GROUP BY does not have to be listed in the SELECT clause; however, it must be a column name from one of the tables listed in the FROM clause.The column name used in a GROUP BY does not have to be listed in the SELECT clause; however, it must be a column name from one of the tables listed in the FROM clause.

Bordoloi and BockExample We could rewrite the last query without specifying the emp_dpt_number column as part of the result table, but as you can see below, the results are rather cryptic without the emp_dpt_number column to identify the meaning of the aggregate count.We could rewrite the last query without specifying the emp_dpt_number column as part of the result table, but as you can see below, the results are rather cryptic without the emp_dpt_number column to identify the meaning of the aggregate count. SELECT COUNT(*) "Department Count" FROM employee GROUP BY emp_dpt_number; Department Count Department Count

Bordoloi and BockExample However, the reverse is NOT true! SELECT emp_dpt_number, COUNT(*) "Department Count" FROM employee; SELECT emp_dpt_number, COUNT(*) "Department Count" * ERROR at line 1: ORA-00937: not a single-group group function

Bordoloi and Bock GROUP BY Clause To keep it simple, just remember the following:To keep it simple, just remember the following: 1. If you have column name(s) AND Aggr. Function(s) in the SELECT clause, then you MUST also have a GROUP BY clause. 2. The column name(s) in the SELECT clause MUST match column name(s) listed in the GROUP BY clause.

Bordoloi and BockExample SELECT emp_dpt_number "Department", emp_gender "Gender", emp_gender "Gender", COUNT(*) "Department Count" COUNT(*) "Department Count" FROM employee GROUP BY emp_dpt_number; ERROR at line 2: ORA-00979: not a GROUP BY expression

Bordoloi and BockExample SELECT emp_dpt_number "Department", emp_gender "G", COUNT(*) “Employee Count" FROM employee GROUP BY emp_dpt_number, emp_gender; Department G Employee Count M 1 1 M 1 3 F 2 3 F 2 3 M 1 3 M 1 7 F 1 7 F 1 7 M 3 7 M 3

Bordoloi and Bock Using GROUP BY With a WHERE Clause The WHERE clause works to eliminates data table rows from consideration before any grouping takes place.The WHERE clause works to eliminates data table rows from consideration before any grouping takes place. The query shown here produces an average hours worked result table for employees with a social security number that is larger than The query shown here produces an average hours worked result table for employees with a social security number that is larger than SELECT work_emp_ssn SSN, AVG(work_hours) "Average Hours Worked" FROM assignment WHERE work_emp_ssn > GROUP BY work_emp_ssn; SSN Average Hours Worked

Bordoloi and Bock Using GROUP BY With an ORDER BY Clause The ORDER BY clause allows you to specify how rows in a result table are sorted.The ORDER BY clause allows you to specify how rows in a result table are sorted. The default ordering is from smallest to largest value.The default ordering is from smallest to largest value. A GROUP BY clause in a SELECT statement will determine the sort order of rows in a result table.A GROUP BY clause in a SELECT statement will determine the sort order of rows in a result table. The sort order can be changed by specifying an ORDER BY clause after the GROUP BY clause.The sort order can be changed by specifying an ORDER BY clause after the GROUP BY clause.

Bordoloi and Bock Using GROUP BY With an ORDER BY Clause SELECT emp_dpt_number "Department", AVG(emp_salary) "Average Salary" FROM employee GROUP BY emp_dpt_number ORDER BY AVG(emp_salary); Department Average Salary $31,000 3 $31,000 7 $34,000 7 $34,000 1 $55,000 1 $55,000

Bordoloi and Bock GROUP BY With a HAVING Clause The HAVING clause is used for aggregate functions in the same way that a WHERE clause is used for column names and expressions.The HAVING clause is used for aggregate functions in the same way that a WHERE clause is used for column names and expressions. The HAVING and WHERE clauses do the same thing, that is filter rows from inclusion in a result table based on a condition.The HAVING and WHERE clauses do the same thing, that is filter rows from inclusion in a result table based on a condition. a WHERE clause is used to filter rows BEFORE the GROUPING action.a WHERE clause is used to filter rows BEFORE the GROUPING action. a HAVING clause filters rows AFTER the GROUPING action.a HAVING clause filters rows AFTER the GROUPING action.

Bordoloi and Bock GROUP BY With a HAVING Clause SELECT emp_dpt_number "Department", AVG(emp_salary) "Average Salary" FROM employee GROUP BY emp_dpt_number HAVING AVG(emp_salary) > 33000; Department Average Salary $55,000 1 $55,000 7 $34,000 7 $34,000

Bordoloi and Bock Combining HAVING Clause with Where clause SELECT emp_dpt_number "Department", AVG(emp_salary) "Average Salary" FROM employee WHERE emp_dpt_number <> 1 GROUP BY emp_dpt_number HAVING AVG(emp_salary) > 33000; Department Average Salary $34,000 7 $34,000

Bordoloi and Bock GROUP BY With a HAVING Clause Conceptually, SQL performs the following steps in the query given above. Conceptually, SQL performs the following steps in the query given above. 1. The WHERE clause filters rows that do not meet the condition emp_dpt_number <> The GROUP BY clause collects the surviving rows into one or more groups for each unique emp_dpt_number. 3. The aggregate function calculates the average salary for each emp_dpt_number grouping. 4. The HAVING clause filters out the rows from the result table that do not meet the condition average salary greater than $33,000.

Bordoloi and Bock More Examples SELECT emp_dpt_number "Department", COUNT(*) "Department Count", COUNT(*) "Department Count", MAX(emp_salary) "Top Salary", MAX(emp_salary) "Top Salary", MIN(emp_salary) "Low Salary" MIN(emp_salary) "Low Salary" FROM employee GROUP BY emp_dpt_number HAVING COUNT(*) >= 3; Department Department Count Top Salary Low Salary $43,000 $25, $43,000 $25, $43,000 $25, $43,000 $25,000

Bordoloi and Bock GROUP BY With a HAVING Clause The HAVING clause is a conditional option that is directly related to the GROUP BY clause option because a HAVING clause eliminates rows from a result table based on the result of a GROUP BY clause.The HAVING clause is a conditional option that is directly related to the GROUP BY clause option because a HAVING clause eliminates rows from a result table based on the result of a GROUP BY clause. In Oracle, A HAVING clause will not work without a GROUP BY clause.In Oracle, A HAVING clause will not work without a GROUP BY clause.

Bordoloi and Bock GROUP BY With a HAVING Clause SELECT emp_dpt_number, AVG(emp_salary) FROM employee HAVING AVG(emp_salary) > 33000; ERROR at line 1: ORA-00937: not a single-group group function