Group Functions Using GROUP BY clause Week 5 – Chapter 5.

Slides:



Advertisements
Similar presentations
Oracle 10g & 11g for Dev Virtual Columns DML error logging
Advertisements

WHERE Clause Chapter 2. Objectives Limit rows by using a WHERE clause Use the LIKE operator Effect of NULL values Use compound conditions Use the BETWEEN.
2 Copyright © 2007, Oracle. All rights reserved. Restricting and Sorting Data.
Copyright  Oracle Corporation, All rights reserved. 4 Aggregating Data Using Group Functions.
 Database is SQL1.mdb ◦ import using MySQL Migration Toolkit 
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.
Set operators (UNION, UNION ALL, MINUS, INTERSECT) [SQL]
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.
LECTURE 10.  Group functions operate on sets of rows to give one result per group.
Introduction to Oracle9i: SQL1 SQL Group Functions.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Download three SQL script files from wiki page.
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-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
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.
Information Resource Engineering SQL4. Recap - Ordering Output  Usually, the order of rows returned in a query result is undefined.  The ORDER BY clause.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Select your database – Your database name is.
Intermediate SQL: Aggregated Data, Joins and Set Operators.
Basic Group Functions (without GROUP BY clause) Week 5 – Chapter 5.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
© Jalal Kawash Database Queries Peeking into Computer Science.
 After completing this lesson, you should be able to do the following: ◦ Use the ROLLUP operation to produce subtotal values ◦ Use the CUBE operation.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Grouping These slides are licensed under.
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.
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.
Chapter 11 – Data Manipulation: Relational Algebra and SQL1 Unit 9: Data Manipulation: Relational Algebra and SQL IT238: Data Modeling and Database Design.
Reporting Aggregated Data Using the Group Functions
Aggregating Data Using Group Functions
Enhanced Guide to Oracle 10g
Chapter 3 Introduction to SQL(3)
Group Functions Lab 6.
Working with Tables: Join, Functions and Grouping
Chapter 5: Aggregate Functions and Grouping of Data
Aggregating Data Using Group Functions
(SQL) Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
SQL – Entire Select.
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Reporting Aggregated Data Using the Group Functions
Query Functions.
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Database Programming Using Oracle 11g
Aggregating Data Using Group Functions
Presentation transcript:

Group Functions Using GROUP BY clause Week 5 – Chapter 5

Objectives Group data using the GROUP BY clause Include or exclude grouped rows results by using the HAVING clause

EMPLOYEES maximum salary in salary in the EMPLOYEES table ID SALARY … MAXSAL Group functions without a GROUP BY clause operate on one set of rows to give one row result Group Fns Without GROUP BY SELECT MAX(salary) MAXSAL FROM employees;

GROUP BY Clause SELECT[column], group_function(column),... FROMtable [WHEREcondition] [GROUP BYcolumn] [HAVING expression] [ORDER BYcolumn]; Divide rows into smaller subsets or groups by using the GROUP BY clause.

EMPLOYEES maximum salary in each dept in salary in each dept in EMPLOYEES table DEPT SALARY … 7000 DEPT MAX … 7000 Group functions with a GROUP BY clause operate on sets of rows to give 1 row result per set of rows Group Fns With GROUP BY SELECT department_id DEPT, MAX(salary) MAX FROM employees GROUP BY department_id;

Notes on GROUP BY Clause A SELECT statement with a group function(s) and no GROUP BY clause returns only one row as its result A SELECT statement with a GROUP BY clause can return multiple rows as its result If you include a group function in a SELECT clause, you cannot display values from individual rows unless the individual column is in the GROUP BY clause The GROUP BY column does not have to be in the SELECT list; eg: SELECT SUM(salary) FROM employees GROUP BY department_id

Group Data on More Than 1 Column Data can be grouped into subsets by more than column. For example, salary could be totaled by job_id within each department as follows: SELECT department_id,job_id,SUM(salary) FROM employees GROUP BY department_id, job_id NOTE: All columns not used with group functions that are in the SELECT clause must be listed in the GROUP BY clause DEPARTMENT_ID JOB_ID SUM(SALARY) SA_REP AD_ASST MK_MAN MK_REP ST_MAN5800 … 110AC_ACCOUNT8300

Group Data on more than 1 Column If the order of the columns is changed in the GROUP BY clause then the data is simply presented in a different order. For example, salary could be totaled by department_id within each job as follows: SELECT job_id, department_id,job_id,SUM(salary) FROM employees GROUP BY job_id, department_id JOB_IDDEPARTMENT_IDSUM(SALARY) AD_VP AC_MGR MK_MAN MK_REP SA_MAN SA_REP7000 SA_REP … AC_ACCOUNT

Group Function Error You will receive an error message if you include a group function and GROUP BY clause and an expression referencing a non-group column in a SELECT clause. Example: SELECTlast_name, department_id, AVG(salary) FROMemployees GROUP BY department_id; ERROR at line 1: ORA-00979: not a GROUP BY expression Query does not make sense: there is only 1 value of department_id and average salary to display for many values of last name

Excluding Group Results Use the HAVING clause to exclude set(s) of results from a GROUP BY clause (rows are grouped; group function is applied; groups matching the HAVING clause are displayed). Example, do not display departments having 2 or less employees: SELECT department_id DEPT, MAX(salary) MAX, COUNT(*) FROM employees GROUP BY department_id HAVING COUNT(*) > 2 DEPT MAXCOUNT(*)

HAVING Clause Error Cannot use the HAVING clause to rows - must reference a group function result in HAVING clause. Example, try to exclude IT_PROG from results: SELECT department_id DEPT, MAX(salary) MAX, COUNT(*) FROM employees GROUP BY department_id HAVING job_id LIKE 'IT_PROG' ERROR at line 4: HAVING job_id LIKE 'IT_PROG' ORA-00979: not a GROUP BY expression

HAVING Clause Notes HAVING clause excludes result(s) for sets of rows. Must be used with GROUP BY clause Should only reference group results (or columns data is grouped by)

Important Notes on Group Functions You cannot use the WHERE clause to restrict groups. You must use the HAVING clause to restrict groups. You cannot use group functions in the WHERE clause (WHERE clause used to limit rows, not groups of rows) Any column or expression in the SELECT list that is not an aggregate function must be in the GROUP BY clause.

Nesting Group Functions  Group functions can be nested  Example: find the highest of the average salary for each department: SELECT MAX(AVG(salary)) MAX FROM employees GROUP BY department_id MAX