SQL WORKSHOP::Lecture 5

26
5 5 Aggregating Data Using Group Functions

Transcript of SQL WORKSHOP::Lecture 5

Page 1: SQL WORKSHOP::Lecture 5

55

Aggregating Data Using Group Functions

Aggregating Data Using Group Functions

Page 2: SQL WORKSHOP::Lecture 5

5-2

ObjectivesObjectives

After completing this lesson, you should After completing this lesson, you should be able to do the following: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

After completing this lesson, you should After completing this lesson, you should be able to do the following: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

Page 3: SQL WORKSHOP::Lecture 5

5-3

What Are Group Functions?What Are Group Functions?Group functions operate on sets of rows to give Group functions operate on sets of rows to give one result per group.one result per group.Group functions operate on sets of rows to give Group functions operate on sets of rows to give one result per group.one result per group.EMPEMP

““maximum maximum salary in salary in

the EMP table”the EMP table”

DEPTNO SAL--------- --------- 10 2450 10 5000 10 1300 20 800 20 1100 20 3000 20 3000 20 2975 30 1600 30 2850 30 1250 30 950 30 1500 30 1250

MAX(SAL)

---------

5000

Page 4: SQL WORKSHOP::Lecture 5

5-4

Types of Group FunctionsTypes of Group Functions

• AVG

• COUNT

• MAX

• MIN

• STDDEV

• SUM

• VARIANCE

• AVG

• COUNT

• MAX

• MIN

• STDDEV

• SUM

• VARIANCE

Page 5: SQL WORKSHOP::Lecture 5

5-5

Using Group FunctionsUsing Group Functions

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

Page 6: SQL WORKSHOP::Lecture 5

5-6

Using AVG and SUM FunctionsUsing AVG and SUM Functions

AVG(SAL) MAX(SAL) MIN(SAL) SUM(SAL)-------- --------- --------- --------- 1400 1600 1250 5600

You can use AVG and SUM for numeric data.You can use AVG and SUM for numeric data.You can use AVG and SUM for numeric data.You can use AVG and SUM for numeric data.

SQL> SELECT AVG(sal), MAX(sal), 2 MIN(sal), SUM(sal) 3 FROM emp 4 WHERE job LIKE 'SALES%';

Page 7: SQL WORKSHOP::Lecture 5

5-7

Using MIN and MAX FunctionsUsing MIN and MAX Functions

You can use MIN and MAX for any datatype.You can use MIN and MAX for any datatype.You can use MIN and MAX for any datatype.You can use MIN and MAX for any datatype.

SQL> SELECT MIN(hiredate), MAX(hiredate) 2 FROM emp;

MIN(HIRED MAX(HIRED--------- ---------17-DEC-80 12-JAN-83

Page 8: SQL WORKSHOP::Lecture 5

5-8

Using the COUNT FunctionUsing the COUNT Function

COUNT(*)--------- 6

SQL> SELECT COUNT(*) 2 FROM emp 3 WHERE deptno = 30;

COUNT(*) returns the number of rows in a COUNT(*) returns the number of rows in a table.table.COUNT(*) returns the number of rows in a COUNT(*) returns the number of rows in a table.table.

Page 9: SQL WORKSHOP::Lecture 5

5-9

Using the COUNT FunctionUsing the COUNT Function

COUNT(COUNT(exprexpr) returns the number of ) returns the number of nonnull rows.nonnull rows.COUNT(COUNT(exprexpr) returns the number of ) returns the number of nonnull rows.nonnull rows.

SQL> SELECT COUNT(comm) 2 FROM emp 3 WHERE deptno = 30;

COUNT(COMM)----------- 4

Page 10: SQL WORKSHOP::Lecture 5

5-10

Group Functions and Null ValuesGroup Functions and Null Values

Group functions ignore null values in the Group functions ignore null values in the column.column.Group functions ignore null values in the Group functions ignore null values in the column.column.

SQL> SELECT AVG(comm) 2 FROM emp;

AVG(COMM)--------- 550

Page 11: SQL WORKSHOP::Lecture 5

5-11

Using the NVL Function with Group Functions

Using the NVL Function with Group Functions

The NVL function forces group functions The NVL function forces group functions to include null values.to include null values.The NVL function forces group functions The NVL function forces group functions to include null values.to include null values.

SQL> SELECT AVG(NVL(comm,0)) 2 FROM emp;

AVG(NVL(COMM,0))---------------- 157.14286

Page 12: SQL WORKSHOP::Lecture 5

5-12

Creating Groups of Data Creating Groups of Data

EMPEMP

““averageaveragesalary salary in EMPin EMPtable table

for each for each department”department”

2916.66672916.6667

21752175

1566.66671566.6667

DEPTNO SAL--------- --------- 10 2450 10 5000 10 1300 20 800 20 1100 20 3000 20 3000 20 2975 30 1600 30 2850 30 1250 30 950 30 1500 30 1250

DEPTNO AVG(SAL)

------- ---------

10 2916.6667

20 2175

30 1566.6667

Page 13: SQL WORKSHOP::Lecture 5

5-13

Creating Groups of Data: GROUP BY Clause

Creating Groups of Data: GROUP BY Clause

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

Divide rows in a table into smaller groups Divide rows in a table into smaller groups by using the GROUP BY clause.by using the GROUP BY clause.Divide rows in a table into smaller groups Divide rows in a table into smaller groups by using the GROUP BY clause.by using the GROUP BY clause.

Page 14: SQL WORKSHOP::Lecture 5

5-14

Using the GROUP BY Clause Using the GROUP BY Clause

All columns in the SELECT list that are not All columns in the SELECT list that are not in group functions must be in the GROUP in group functions must be in the GROUP BY clause.BY clause.

All columns in the SELECT list that are not All columns in the SELECT list that are not in group functions must be in the GROUP in group functions must be in the GROUP BY clause.BY clause.

SQL> SELECT deptno, AVG(sal) 2 FROM emp 3 GROUP BY deptno;

DEPTNO AVG(SAL)--------- --------- 10 2916.6667 20 2175 30 1566.6667

Page 15: SQL WORKSHOP::Lecture 5

5-15

Using the GROUP BY Clause Using the GROUP BY Clause

The GROUP BY column does not have to The GROUP BY column does not have to be in the SELECT list.be in the SELECT list.The GROUP BY column does not have to The GROUP BY column does not have to be in the SELECT list.be in the SELECT list.

SQL> SELECT AVG(sal) 2 FROM emp 3 GROUP BY deptno;

AVG(SAL)--------- 2916.6667 21751566.6667

Page 16: SQL WORKSHOP::Lecture 5

5-16

Grouping by More Than One ColumnGrouping by More Than One Column

EMPEMP

““sum salaries in sum salaries in the EMP tablethe EMP tablefor each job, for each job, grouped by grouped by department”department”

DEPTNO JOB SAL

--------- --------- ---------

10 MANAGER 2450

10 PRESIDENT 5000

10 CLERK 1300

20 CLERK 800

20 CLERK 1100

20 ANALYST 3000

20 ANALYST 3000

20 MANAGER 2975

30 SALESMAN 1600

30 MANAGER 2850

30 SALESMAN 1250

30 CLERK 950

30 SALESMAN 1500

30 SALESMAN 1250

JOB SUM(SAL)

--------- ---------

CLERK 1300

MANAGER 2450

PRESIDENT 5000

ANALYST 6000

CLERK 1900

MANAGER 2975

CLERK 950

MANAGER 2850

SALESMAN 5600

DEPTNO

--------

10

10

10

20

20

20

30

30

30

Page 17: SQL WORKSHOP::Lecture 5

5-17

Using the GROUP BY Clause on Multiple Columns

Using the GROUP BY Clause on Multiple Columns

SQL> SELECT deptno, job, sum(sal) 2 FROM emp 3 GROUP BY deptno, job;

DEPTNO JOB SUM(SAL)--------- --------- --------- 10 CLERK 1300 10 MANAGER 2450 10 PRESIDENT 5000 20 ANALYST 6000 20 CLERK 1900...9 rows selected.

Page 18: SQL WORKSHOP::Lecture 5

5-18

Illegal Queries Using Group Functions

Illegal Queries Using Group Functions

Any column or expression in the SELECT Any column or expression in the SELECT list that is not an aggregate function must list that is not an aggregate function must be in the GROUP BY clause.be in the GROUP BY clause.

Any column or expression in the SELECT Any column or expression in the SELECT list that is not an aggregate function must list that is not an aggregate function must be in the GROUP BY clause.be in the GROUP BY clause.

SQL> SELECT deptno, COUNT(ename) 2 FROM emp;

SQL> SELECT deptno, COUNT(ename) 2 FROM emp;

SELECT deptno, COUNT(ename) *ERROR at line 1:ORA-00937: not a single-group group function

SELECT deptno, COUNT(ename) *ERROR at line 1:ORA-00937: not a single-group group function

Column missing in the GROUP BY clause

Column missing in the GROUP BY clause

Column missing in the GROUP BY clause

Column missing in the GROUP BY clause

Page 19: SQL WORKSHOP::Lecture 5

5-19

Illegal Queries Using Group Functions

Illegal Queries Using Group Functions

• You cannot use the WHERE clause to restrict groups.

• You use the HAVING clause to restrict groups.

• You cannot use the WHERE clause to restrict groups.

• You use the HAVING clause to restrict groups.

SQL> SELECT deptno, AVG(sal) 2 FROM emp 3 WHERE AVG(sal) > 2000 4 GROUP BY deptno;

SQL> SELECT deptno, AVG(sal) 2 FROM emp 3 WHERE AVG(sal) > 2000 4 GROUP BY deptno;

WHERE AVG(sal) > 2000 *ERROR at line 3:ORA-00934: group function is not allowed here

WHERE AVG(sal) > 2000 *ERROR at line 3:ORA-00934: group function is not allowed here

Cannot use the WHERE clause

Cannot use the WHERE clause

to

restrict groups

to restrict groups

Cannot use the WHERE clause

Cannot use the WHERE clause

to

restrict groups

to restrict groups

Page 20: SQL WORKSHOP::Lecture 5

5-20

Excluding Group ResultsExcluding Group Results

““maximummaximumsalarysalary

per departmentper departmentgreater thangreater than

$2900”$2900”

EMPEMP

50005000

30003000

28502850

DEPTNO SAL

--------- ---------

10 2450

10 5000

10 1300

20 800

20 1100

20 3000

20 3000

20 2975

30 1600

30 2850

30 1250

30 950

30 1500

30 1250

DEPTNO MAX(SAL)

--------- ---------

10 5000

20 3000

Page 21: SQL WORKSHOP::Lecture 5

5-21

Excluding Group Results: HAVING Clause

Excluding Group Results: HAVING Clause

Use the HAVING clause to restrict groupsUse the HAVING clause to restrict groups

– Rows are grouped.

– The group function is applied.

– Groups matching the HAVING clause are displayed.

Use the HAVING clause to restrict groupsUse the HAVING clause to restrict groups

– Rows are grouped.

– The group function is applied.

– Groups matching the HAVING clause are displayed.

SELECT column, group_functionFROM table[WHERE condition][GROUP BY group_by_expression][HAVING group_condition][ORDER BY column];

Page 22: SQL WORKSHOP::Lecture 5

5-22

Using the HAVING ClauseUsing the HAVING Clause

SQL> SELECT deptno, max(sal) 2 FROM emp 3 GROUP BY deptno 4 HAVING max(sal)>2900;

DEPTNO MAX(SAL)--------- --------- 10 5000 20 3000

Page 23: SQL WORKSHOP::Lecture 5

5-23

Using the HAVING ClauseUsing the HAVING Clause

SQL> SELECT job, SUM(sal) PAYROLL 2 FROM emp 3 WHERE job NOT LIKE 'SALES%' 4 GROUP BY job 5 HAVING SUM(sal)>5000 6 ORDER BY SUM(sal);

JOB PAYROLL--------- ---------ANALYST 6000MANAGER 8275

Page 24: SQL WORKSHOP::Lecture 5

5-24

Nesting Group FunctionsNesting Group Functions

SQL> SELECT max(avg(sal)) 2 FROM emp 3 GROUP BY deptno;

MAX(AVG(SAL))------------- 2916.6667

Display the maximum average salary. Display the maximum average salary. Display the maximum average salary. Display the maximum average salary.

Page 25: SQL WORKSHOP::Lecture 5

5-25

SummarySummarySELECT column, group_function(column)FROM table[WHERE condition][GROUP BY group_by_expression][HAVING group_condition][ORDER BY column];

Order of evaluation of the clauses:Order of evaluation of the clauses:

• WHERE clause

• GROUP BY clause

• HAVING clause

Order of evaluation of the clauses:Order of evaluation of the clauses:

• WHERE clause

• GROUP BY clause

• HAVING clause

Page 26: SQL WORKSHOP::Lecture 5

5-26

Practice OverviewPractice Overview

• Showing different queries that use group functions

• Grouping by rows to achieve more than one result

• Excluding groups by using the HAVING clause

• Showing different queries that use group functions

• Grouping by rows to achieve more than one result

• Excluding groups by using the HAVING clause