5 5 Aggregating Data Using Group Functions Important Legal Notice: Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan (1999), published by Oracle Corp. For further information, visit This presentation must be used for only education purpose for students at Central Washington University which is a member of Oracle Academic Initiatives (OAI) and has used Oracle systems for HRIS & Accounting Systems as a database platform embedded on its PeopleSoft ERP system, since 1999.
Objectives After completing this lesson, you should be able to do the following: Identify the available group functions Describe the use of group functions Group data using the GROUP BY clause Include or exclude grouped rows by using the HAVING clause
What are Group Functions? “maximum salary in the EMP table” MAX(SAL) Group functions operate on sets of rows to give one result per group.
Types of Group Functions AVG COUNT MAX MIN STDDEV SUM VARIANCE
Using Group Functions SELECT[column,] group_function(column) FROMtable [WHEREcondition] [GROUP BYcolumn] [HAVING ….] [ORDER BYcolumn];
Using AVG and SUM Functions SQL>SELECTAVG(sal), MAX(sal), MIN(sal), SUM(sal) 3FROMemp 4WHEREjob LIKE ‘SALES%’; AVG(SAL)MAX(SAL)MIN(SAL)SUM(SAL) You can use AVG and SUM for numeric data.
Using MIN and MAX Functions SQL>SELECTMIN(hiredate), MAX(hiredate) 2FROMemp; MIN(HIREDMAX(HIRED DEC-8012-JAN-83 You can use MIN and MAX for any datatype.
Using the COUNT Function COUNT(*) SQL>SELECTCOUNT(*) 2FROMemp 3WHEREdeptno = 30; COUNT (*) returns the number of rows in a table.
Using the COUNT Function COUNT(COMM) SQL>SELECTCOUNT(comm) 2FROMemp 3WHEREdeptno = 30; COUNT(expr) returns the number of non-null rows.
Group Functions and Null Values SQL>SELECTAVG(comm) 2FROM emp; AVG(COMM) Group functions ignore null values in the column.
Using the NVL Function with Group Functions SQL>SELECTAVG(NVL(comm,0)) 2FROMemp; AVG(NVL(COMM,0)) The NVL function forces group functions to include null values.
Creating Groups of Data DEPTNOSAL “average salary in EMP table for each department” DEPTNOAVG(SAL)
Creating Groups of Data: GROUP BY Clause SELECT[column,] group_function(column) FROMtable [WHEREcondition] [GROUP BYgroup_by_expression] [ORDER BYcolumn]; Divide rows in a table into smaller groups by using the GROUP BY clause.
Using the GROUP BY Clause DEPTNOAVG(SAL) SQL>SELECTdeptno, AVG(sal) 2FROMemp 3GROUP BYdeptno; All columns in the SELECT list that are not in group functions must be in the GROUP BY clause.
Using the GROUP BY Clause AVG(SAL) SQL>SELECTAVG(sal) 2FROMemp 3GROUP BYdeptno; The GROUP BY column does not have to be in the SELECT list.
Grouping by More Than One Column DEPTNOJOBSAL MANAGER PRESIDENT CLERK CLERK800 20CLERK ANALYST ANALYST MANAGER SALESMAN MANAGER SALESMAN CLERK950 30SALESMAN SALESMAN rows selected. EMP “ sum salaries in the EMP table for each job, grouped by department.” DEPTNOJOBSUM(SAL) CLERK MANAGER PRESIDENT ANALYST CLERK MANAGER CLERK MANAGER SALESMAN 5600
Using the GROUP BY Clause on Multiple Columns DEPTNOJOB CLERK 10MANAGER 10PRESIDENT 20ANALYST 20CLERK... 9 rows selected. SQL>SELECTdeptno, job, sum(sal) 2FROMemp 3GROUP BYdeptno, job; SUM(SAL)
Illegal Queries Using Group Functions Any column or expression in the SELECT list that is not an aggregate function must be in the GROUP BY clause. SQL>SELECTdeptno, COUNT(ename) 2FROMemp; SELECTdeptno, COUNT(ename) * ERROR at line 1: ORA-00937: not a single-group group function Column is missing in the GROUP BY clause
Illegal Queries Using Group Functions You cannot use the WHERE clause to restrict groups. You use the HAVING clause to restrict groups. SQL>SELECTdeptno, AVG(sal) 2FROMemp 3WHEREAVG(sal) > GROUP BYdeptno; WHEREAVG (sal) > 2000 * ERROR at line 3: ORA-00934: group function is not allowed here Cannot use the WHERE clause to restrict groups
Excluding Group Results DEPTNOSAL “maximum salary per department greater than $2900” DEPTNOMAX(SAL)
Excluding Group Results: HAVING Clause Use the HAVING clause to restrict groups Rows are grouped. The group function is applied. Groups matching the HAVING clause are displayed. SELECTcolumn, group_function FROMtable [WHEREcondition] [GROUP BYgroup_by_expression] [HAVINGgroup_condition] [ORDER BYcolumn];
Using the HAVING Clause MAX(SAL) SQL>SELECTdeptno, max(sal) 2FROMemp 3GROUP BYdeptno 4HAVINGmax(sal)>2900; DEPTNO
Using the HAVING Clause SQL>SELECTjob, SUM(sal) PAYROLL 2FROMemp 3WHEREjob NOT LIKE ‘SALES%’ 4GROUP BYjob 5HAVINGSUM(sal) > ORDER BYSUM(sal); JOB ANALYST MANAGER PAYROLL
Nesting Group Functions MAX(AVG(SAL) ) SQL>SELECTmax(avg(sal)) 2FROMemp 3GROUP BYdeptno; Display the maximum average salary.
Summary SELECTcolumn, group_function(column) FROMtable [WHEREcondition] [GROUP BYgroup_by_expression] [HAVINGgroup_condition] [ORDER BYcolumn]; Order of evaluation of the clauses: WHERE clause GROUP BY clause HAVING clause
Practice Overview Showing different queries that use group functions Grouping by rows to achieve more than one result Excluding groups by using the HAVING clause