Aggregating Data Using Group Functions
ObjectivesAfter completing this lesson, you should be able to do the following:Identify the available group functionsDescribe the use of group functionsGroup data using the GROUP BY clauseInclude or exclude grouped rows by using the HAVING clause
What Are Group Functions?Group functions operate on sets of rows to give one result per group.EMP   DEPTNO       SAL--------- ---------1024501050001013002080020110020300020300020297530160030285030125030950301500301250 MAX(SAL)---------5000“maximum    salary in the EMP table”
Types of Group FunctionsAVG COUNT MAXMIN STDDEV SUMVARIANCE
Using Group FunctionsSELECT	[column,] group_function(column)FROMtable[WHEREcondition][GROUP BYcolumn][ORDER BYcolumn];
Using AVG and SUM FunctionsYou can use AVG and SUM for numeric data.SQL> SELECTAVG(sal), MAX(sal),2MIN(sal), SUM(sal)3FROMemp4WHEREjob LIKE 'SALES%';AVG(SAL)  MAX(SAL)  MIN(SAL)  SUM(SAL)-------- --------- --------- ---------1400160012505600
Using MIN and MAX FunctionsYou can use MIN and MAX for any datatype.SQL> SELECTMIN(hiredate), MAX(hiredate)2  FROMemp;MIN(HIRED MAX(HIRED--------- ---------17-DEC-8012-JAN-83
Using the COUNT FunctionCOUNT(*) returns the number of rows in a table.SQL> SELECTCOUNT(*)2  FROMemp3  WHEREdeptno = 30; COUNT(*)---------6
Using the COUNT FunctionCOUNT(expr) returns the number of nonnull rows.SQL> SELECTCOUNT(comm)2  FROMemp3  WHEREdeptno = 30;COUNT(COMM)-----------4
Group Functions and Null ValuesGroup functions ignore null values in the column.SQL> SELECT AVG(comm)2  FROM   emp; AVG(COMM)---------550
Using the NVL Function with Group FunctionsThe NVL function forces group functions to include null values.SQL> SELECT AVG(NVL(comm,0))2  FROM   emp;AVG(NVL(COMM,0))----------------157.14286
2916.666721751566.6667Creating Groups of Data EMP   DEPTNO       SAL--------- ---------1024501050001013002080020110020300020300020297530160030285030125030950301500301250   DEPTNO  AVG(SAL)  ------- ---------102916.6667202175301566.6667“averagesalary in EMPtable for each department”
Creating Groups of Data: GROUP BY ClauseSELECTcolumn, 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 All columns in the SELECT list that are not in group functions must be in the GROUP BY clause.SQL> SELECT   deptno, AVG(sal)2  FROM     emp3  GROUP BY deptno;   DEPTNO  AVG(SAL)--------- ---------102916.6667202175301566.6667
Using the GROUP BY Clause The GROUP BY column does not have to be in the SELECT list.SQL> SELECT   AVG(sal)2  FROM     emp3  GROUP BY deptno; AVG(SAL)--------- 2916.666721751566.6667
Grouping by More Than One ColumnEMP   DEPTNO JOB             SAL--------- --------- ---------10 MANAGER        245010 PRESIDENT      500010 CLERK          130020 CLERK           80020 CLERK          110020 ANALYST        300020 ANALYST        300020 MANAGER        297530 SALESMAN       160030 MANAGER        285030 SALESMAN       125030 CLERK           95030 SALESMAN       150030 SALESMAN       1250JOB        SUM(SAL)--------- ---------CLERK          1300MANAGER        2450PRESIDENT      5000ANALYST        6000CLERK          1900MANAGER        2975CLERK           950MANAGER        2850SALESMAN       5600DEPTNO--------101010202020303030“sum salaries in the EMP tablefor each job, grouped by department”
Using the GROUP BY Clause on Multiple ColumnsSQL> SELECT   deptno, job, sum(sal)2  FROM     emp3  GROUP BY deptno, job;   DEPTNO JOB        SUM(SAL)--------- --------- ---------10 CLERK          130010 MANAGER        245010 PRESIDENT      500020 ANALYST        600020 CLERK          1900...9 rows selected.
Illegal Queries Using Group FunctionsAny column or expression in the SELECT list that is not an aggregate function must be in the GROUP BY clause.SQL> SELECTdeptno, COUNT(ename)2  FROMemp;Column missing in the GROUP BY clauseSELECT deptno, COUNT(ename)       *ERROR at line 1:ORA-00937: not a single-group group function
Illegal Queries Using Group FunctionsYou cannot use the WHERE clause to restrict groups.You use the HAVING clause to restrict groups.SQL> SELECT deptno, AVG(sal)2  FROM emp3  WHERE AVG(sal) > 20004  GROUP BY deptno;Cannot use the WHERE clause            to restrict groupsWHERE AVG(sal) > 2000      *ERROR at line 3:ORA-00934: group function is not allowed here
Excluding Group Results500030002850EMP   DEPTNO       SAL--------- ---------1024501050001013002080020110020300020300020297530160030285030125030950301500301250“maximumsalaryper departmentgreater than$2900”   DEPTNO  MAX(SAL)--------- ---------105000203000
Excluding Group Results: HAVING ClauseUse the HAVING clause to restrict groupsRows are grouped.The group function is applied.Groups matching the HAVING clause are displayed.SELECTcolumn, group_functionFROMtable[WHEREcondition][GROUP BYgroup_by_expression][HAVINGgroup_condition][ORDER BYcolumn];
Using the HAVING ClauseSQL> SELECT   deptno, max(sal)2  FROM     emp3  GROUP BY deptno4  HAVING   max(sal)>2900;   DEPTNO  MAX(SAL)--------- ---------105000203000
Using the HAVING ClauseSQL> SELECT    job, SUM(sal) PAYROLL2  FROM      emp3  WHERE  job NOT LIKE 'SALES%'4  GROUP BY  job5  HAVING    SUM(sal)>50006  ORDER BY  SUM(sal);JOB         PAYROLL--------- ---------ANALYST        6000MANAGER        8275
Nesting Group FunctionsDisplay the maximum average salary. SQL> SELECT   max(avg(sal))2  FROM     emp3  GROUP BY deptno;MAX(AVG(SAL))-------------2916.6667
SummarySELECTcolumn, group_function(column)FROMtable[WHEREcondition][GROUP BYgroup_by_expression][HAVINGgroup_condition][ORDER BYcolumn];Order of evaluation of the clauses:WHERE clauseGROUP BY clauseHAVING clause
Practice OverviewShowing different queries that use group functionsGrouping by rows to achieve more than one resultExcluding groups by using the HAVING clause

Les05 Aggregating Data Using Group Function

  • 1.
    Aggregating Data UsingGroup Functions
  • 2.
    ObjectivesAfter completing thislesson, you should be able to do the following:Identify the available group functionsDescribe the use of group functionsGroup data using the GROUP BY clauseInclude or exclude grouped rows by using the HAVING clause
  • 3.
    What Are GroupFunctions?Group functions operate on sets of rows to give one result per group.EMP DEPTNO SAL--------- ---------1024501050001013002080020110020300020300020297530160030285030125030950301500301250 MAX(SAL)---------5000“maximum salary in the EMP table”
  • 4.
    Types of GroupFunctionsAVG COUNT MAXMIN STDDEV SUMVARIANCE
  • 5.
    Using Group FunctionsSELECT [column,]group_function(column)FROMtable[WHEREcondition][GROUP BYcolumn][ORDER BYcolumn];
  • 6.
    Using AVG andSUM FunctionsYou can use AVG and SUM for numeric data.SQL> SELECTAVG(sal), MAX(sal),2MIN(sal), SUM(sal)3FROMemp4WHEREjob LIKE 'SALES%';AVG(SAL) MAX(SAL) MIN(SAL) SUM(SAL)-------- --------- --------- ---------1400160012505600
  • 7.
    Using MIN andMAX FunctionsYou can use MIN and MAX for any datatype.SQL> SELECTMIN(hiredate), MAX(hiredate)2 FROMemp;MIN(HIRED MAX(HIRED--------- ---------17-DEC-8012-JAN-83
  • 8.
    Using the COUNTFunctionCOUNT(*) returns the number of rows in a table.SQL> SELECTCOUNT(*)2 FROMemp3 WHEREdeptno = 30; COUNT(*)---------6
  • 9.
    Using the COUNTFunctionCOUNT(expr) returns the number of nonnull rows.SQL> SELECTCOUNT(comm)2 FROMemp3 WHEREdeptno = 30;COUNT(COMM)-----------4
  • 10.
    Group Functions andNull ValuesGroup functions ignore null values in the column.SQL> SELECT AVG(comm)2 FROM emp; AVG(COMM)---------550
  • 11.
    Using the NVLFunction with Group FunctionsThe NVL function forces group functions to include null values.SQL> SELECT AVG(NVL(comm,0))2 FROM emp;AVG(NVL(COMM,0))----------------157.14286
  • 12.
    2916.666721751566.6667Creating Groups ofData EMP DEPTNO SAL--------- ---------1024501050001013002080020110020300020300020297530160030285030125030950301500301250 DEPTNO AVG(SAL) ------- ---------102916.6667202175301566.6667“averagesalary in EMPtable for each department”
  • 13.
    Creating Groups ofData: GROUP BY ClauseSELECTcolumn, 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.
  • 14.
    Using the GROUPBY Clause All columns in the SELECT list that are not in group functions must be in the GROUP BY clause.SQL> SELECT deptno, AVG(sal)2 FROM emp3 GROUP BY deptno; DEPTNO AVG(SAL)--------- ---------102916.6667202175301566.6667
  • 15.
    Using the GROUPBY Clause The GROUP BY column does not have to be in the SELECT list.SQL> SELECT AVG(sal)2 FROM emp3 GROUP BY deptno; AVG(SAL)--------- 2916.666721751566.6667
  • 16.
    Grouping by MoreThan One ColumnEMP DEPTNO JOB SAL--------- --------- ---------10 MANAGER 245010 PRESIDENT 500010 CLERK 130020 CLERK 80020 CLERK 110020 ANALYST 300020 ANALYST 300020 MANAGER 297530 SALESMAN 160030 MANAGER 285030 SALESMAN 125030 CLERK 95030 SALESMAN 150030 SALESMAN 1250JOB SUM(SAL)--------- ---------CLERK 1300MANAGER 2450PRESIDENT 5000ANALYST 6000CLERK 1900MANAGER 2975CLERK 950MANAGER 2850SALESMAN 5600DEPTNO--------101010202020303030“sum salaries in the EMP tablefor each job, grouped by department”
  • 17.
    Using the GROUPBY Clause on Multiple ColumnsSQL> SELECT deptno, job, sum(sal)2 FROM emp3 GROUP BY deptno, job; DEPTNO JOB SUM(SAL)--------- --------- ---------10 CLERK 130010 MANAGER 245010 PRESIDENT 500020 ANALYST 600020 CLERK 1900...9 rows selected.
  • 18.
    Illegal Queries UsingGroup FunctionsAny column or expression in the SELECT list that is not an aggregate function must be in the GROUP BY clause.SQL> SELECTdeptno, COUNT(ename)2 FROMemp;Column missing in the GROUP BY clauseSELECT deptno, COUNT(ename) *ERROR at line 1:ORA-00937: not a single-group group function
  • 19.
    Illegal Queries UsingGroup FunctionsYou cannot use the WHERE clause to restrict groups.You use the HAVING clause to restrict groups.SQL> SELECT deptno, AVG(sal)2 FROM emp3 WHERE AVG(sal) > 20004 GROUP BY deptno;Cannot use the WHERE clause to restrict groupsWHERE AVG(sal) > 2000 *ERROR at line 3:ORA-00934: group function is not allowed here
  • 20.
    Excluding Group Results500030002850EMP DEPTNO SAL--------- ---------1024501050001013002080020110020300020300020297530160030285030125030950301500301250“maximumsalaryper departmentgreater than$2900” DEPTNO MAX(SAL)--------- ---------105000203000
  • 21.
    Excluding Group Results:HAVING ClauseUse the HAVING clause to restrict groupsRows are grouped.The group function is applied.Groups matching the HAVING clause are displayed.SELECTcolumn, group_functionFROMtable[WHEREcondition][GROUP BYgroup_by_expression][HAVINGgroup_condition][ORDER BYcolumn];
  • 22.
    Using the HAVINGClauseSQL> SELECT deptno, max(sal)2 FROM emp3 GROUP BY deptno4 HAVING max(sal)>2900; DEPTNO MAX(SAL)--------- ---------105000203000
  • 23.
    Using the HAVINGClauseSQL> SELECT job, SUM(sal) PAYROLL2 FROM emp3 WHERE job NOT LIKE 'SALES%'4 GROUP BY job5 HAVING SUM(sal)>50006 ORDER BY SUM(sal);JOB PAYROLL--------- ---------ANALYST 6000MANAGER 8275
  • 24.
    Nesting Group FunctionsDisplaythe maximum average salary. SQL> SELECT max(avg(sal))2 FROM emp3 GROUP BY deptno;MAX(AVG(SAL))-------------2916.6667
  • 25.
    SummarySELECTcolumn, group_function(column)FROMtable[WHEREcondition][GROUP BYgroup_by_expression][HAVINGgroup_condition][ORDERBYcolumn];Order of evaluation of the clauses:WHERE clauseGROUP BY clauseHAVING clause
  • 26.
    Practice OverviewShowing differentqueries that use group functionsGrouping by rows to achieve more than one resultExcluding groups by using the HAVING clause