LECTURE 10.  Group functions operate on sets of rows to give one result per group.

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)
5 5 Aggregating Data Using Group Functions Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina,
Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
AGGREGATE FUNCTIONS Prof. Sin-Min Lee Surya Bhagvat CS 157A – Fall 2005.
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.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: Identify the available group.
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
After completing this lesson, you should be able to do the following: Identify the available group functions Describe the use of group functions Group.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
Introduction to Oracle9i: SQL1 SQL Group Functions.
Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Enhancements to the GROUP BY Clause Fresher Learning Program January, 2012.
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.
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
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.
Intermediate SQL: Aggregated Data, Joins and Set Operators.
Basic Group Functions (without GROUP BY clause) Week 5 – Chapter 5.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
1 Querying a Single Table Structured Query Language (SQL) - Part II.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
Retrieving Data Using the SQL SELECT Statement. Objectives After completing this lesson, you should be able to do the following: – List the capabilities.
1 SQL SQL (Structured Query Language) : is a database language that is used to create, modify and update database design and data. Good Example of DBMS’s.
15 Copyright © Oracle Corporation, All rights reserved. Using SET Operators.
Copyright © 2004, Oracle. All rights reserved. Using the Set Operators.
SQL Aggregation Oracle and ANSI Standard SQL Lecture 9.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: –Identify the available group.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
1 Introduction to Database Systems, CS420 SQL JOIN, Group-by and Sub-query Clauses.
5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.
Reporting Aggregated Data Using the Group Functions
Restricting and Sorting Data
Aggregating Data Using Group Functions
Enhanced Guide to Oracle 10g
Group Functions Lab 6.
Working with Tables: Join, Functions and Grouping
Using the Set Operators
Aggregating Data Using Group Functions
Writing Basic SQL SELECT Statements
(SQL) Aggregating Data Using Group Functions
Using the Set Operators
Aggregating Data Using Group Functions
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL
Chapter 4 Summary Query.
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Writing Basic SQL SELECT Statements
Reporting Aggregated Data Using the Group Functions
Section 4 - Sorting/Functions
Reporting Aggregated Data Using the Group Functions
Using the Set Operators
Reporting Aggregated Data Using the Group Functions
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Aggregating Data Using Group Functions
Presentation transcript:

LECTURE 10

 Group functions operate on sets of rows to give one result per group.

DescriptionGroup Function Average value of n, ignoring null valuesAVG ([DISTINCT|ALL] n) Number of rows (count all selected rows using *, including duplicates and rows with nulls) COUNT ({*| [DISTINCT| ALL] expr}) Maximum value of expr, ignoring null values MAX([DISTINCT|ALL] expr) Minimum value of expr, ignoring null values MIN([DISTINCT|ALL] expr) Sum values of n, ignoring null valuesSUM( [DISTINCT|ALL] n)

SELECT [column,] group_function(column),... FROM table [WHERE condition] [GROUP BY column] [ORDER BY column];

 DISTINCT makes the function consider only non- duplicate values; ALL makes it consider every value including duplicates. “The default is ALL and therefore does not need to be specified.”  The data types for the functions  with an expr argument may be CHAR, VARCHAR2, NUMBER, or DATE.  With n argument can be only NUMBER.  All group functions ignore null values..

 You can use the MIN and MAX functions for any data type.  The previews slide example displays the most junior and most senior employee.  The following example displays the employee last name that is first and the employee last name that is the last in an alphabetized list of all employees.  SELECT MIN(last_name), MAX(last_name) FROM employees;

 Note: AVG, SUM functions can be used only with numeric data types.

 Group functions ignore null values in the column. For example: SELECT AVG(commission_pct) FROM employees;  The average is calculated as the total commission paid to all employees divided by the number of employees receiving a commission.

Until now, all group functions have treated the table as one large group of information. At times, you need to divide the table of information into smaller groups. This can be done by using the GROUP BY clause.

 If you include a group function in a SELECT clause, you cannot select individual results as well, unless the individual column appears in the GROUP BY clause  You’ll receive an error message if you fail to include the column list in the GROUP BY clause.  Using a WHERE clause, you can exclude rows before dividing them into groups  executed first (before group by)  You cannot use a column alias in the GROUP BY clause.  By default, rows are sorted by ascending order of the columns included in the GROUP BY list. You can override this by using the ORDER BY clause.

 Sometimes you need to see results for groups within groups.  Example 1 shows a report that displays the total salary being paid to each job title, within each department.

 The EMPLOYEES table is grouped first by department number and then, within that grouping, by job title.  For example, the four stock clerks in department 50 are grouped together and a single result (total salary) is produced for all stock clerks within the group.

 You can return summary results for groups and subgroups by listing more than one GROUP BY column. You can determine the default sort order of the results by the order of the columns in the GROUP BY clause. Here is how the SELECT statement on the slide, containing a GROUP BY clause, is evaluated:  The SELECT clause specifies the column to be retrieved: – Department number in the EMPLOYEES table – Job ID in the EMPLOYEES table – The sum of all the salaries in the group that you specified in the GROUP BY clause  The FROM clause specifies the tables that the database must access: the EMPLOYEES table  The GROUP BY clause specifies how you must group the rows: – First, the rows are grouped by department number – Second, within the department number groups, the rows are grouped by job ID So the SUM function is being applied to the salary column for all job IDs within each department number group.

 Any column or expression in the SELECT list that is not an aggregate function must be in the GROUP BY clause.  SELECT department_id, COUNT(last_name) FROM employees;  Column missing in the GROUP BY clause SELECT department_id, COUNT(last_name) * ERROR at line 1: ORA-00937: not a single-group group function

 You cannot use the WHERE clause to restrict groups.  You use the HAVING clause to restrict groups.  You cannot use group functions in the WHERE clause. SELECT department_id, AVG(salary) FROM employees WHERE AVG(salary) > 8000 GROUP BY department_id; Error *  You can correct the error by using the HAVING clause to restrict groups. SELECT department_id, AVG(salary) FROM employees HAVING AVG(salary) > 8000 GROUP BY department_id;

 Use the HAVING clause to restrict groups: 1. Rows are grouped. 2. The group function is applied. 3. Groups matching the HAVING clause are displayed. SELECT column, group_function FROM table [WHERE condition] [GROUP BY group_by_expression] [HAVING group_condition] [ORDER BY column];

SELECT job_id, SUM(salary) PAYROLL FROM employees WHERE job_id NOT LIKE '%REP%' GROUP BY job_id HAVING SUM(salary) > ORDER BY SUM(salary);