Aggregating and Grouping Example
Aggregating and Grouping Example
10 2450
10 5000
10 1300
20 800
20 1100
20 3000 “maximum MAX(SAL)
20 3000 salary in ---------
20 2975 the EMP table” 5000
30 1600
30 2850
30 1250
30 950
30 1500
30 1250
5-3
2
Types of Group Functions
• AVG
• COUNT
• MAX
• MIN
• STDDEV
• SUM
• VARIANCE
5-4
Group Functions
Each of the functions accepts an argument. The following table identifies the options that you can use in the
syntax:
Function Description
AVG([DISTINCT|ALL]n) Average value of n, ignoring null values
COUNT({*|[DISTINCT|ALL]expr}) Number of rows, where expr evaluates to something other
than null (Count all selected rows using *, including
duplicates and rows with nulls.)
MAX([DISTINCT|ALL]expr) Maximum value of expr, ignoring null values
3
Using Group Functions
5-5
4
Using AVG and SUM Functions
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%';
5-6
Group Functions
You can use AVG, SUM, MIN, and MAX functions against columns that can store numeric data. The
example on the slide displays the average, highest, lowest and sum of monthly salaries for all salespeople.
5
Using MIN and MAX Functions
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
5-7
Group Functions
You can use MAX and MIN functions for any datatype. The slide example displays the most junior and most
senior employee.
The following example displays the employee name that is first and the employee name that is the last in an
alphabetized list of all employees.
MIN(ENAME) MAX(ENAME)
ADAMS WARD
Note: AVG, SUM, VARIANCE, and STDDEV functions can be used only with numeric datatypes.
6
Using the COUNT Function
COUNT(*) returns the number of rows in a
table.
SQL> SELECT COUNT(*)
2 FROM emp
3 WHERE deptno = 30;
COUNT(*)
5-8
7
Using the COUNT Function
COUNT(expr) returns the number of
nonnull rows.
SQL> SELECT COUNT(comm)
2 FROM emp
3 WHERE deptno = 30;
COUNT(COMM)
5-9
14
Display the number of distinct departments in the EMP table.
COUNT(DISTINCT(DEPTNO))
3
8
Group Functions and Null Values
Group functions ignore null values in the
column.
SQL> SELECT AVG(comm)
2 FROM emp;
AVG(COMM)
550
5-10
9
Using the NVL Function
with Group Functions
The NVL function forces group functions
to include null values.
AVG(NVL(COMM,0))
157.14286
5-11
10
Creating Groups of Data
EMP
DEPTNO SAL
10 2450
10 5000 2916.6667
10 1300
“average DEPTNO AVG(SAL)
20 800
20 1100 salary
------- ---------
20 3000 2175 in EMP
table 10 2916.6667
20 3000
20 2975 for each 20 2175
30 1600 department” 30 1566.6667
30 2850
30 1250 1566.6667
30 950
30 1500
30 1250
5-12
Groups of Data
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.
11
Creating Groups of Data:
GROUP BY Clause
5-13
12
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 emp
3 GROUP BY deptno;
DEPTNO AVG(SAL)
10 2916.6667
20 2175
30 1566.6667
5-14
13
Using the GROUP BY Clause
The GROUP BY column does not have to
be in the SELECT list.
SQL> SELECT AVG(sal)
2 FROM emp
3 GROUP BY deptno;
AVG(SAL)
2916.6667
2175
1566.6667
5-15
DEPTNO AVG(SAL)
30 1566.6667
20 2175
10 2916.6667
14
Grouping by More
EMP
Than One Column
DEPTNO JOB SAL
10 MANAGER 2450
DEPTNO JOB SUM(SAL)
10 PRESIDENT 5000
-------- --------- ---------
10 CLERK 1300
10 CLERK 1300
20 CLERK 800 “sum salaries in 10 MANAGER 2450
20 CLERK 1100 the EMP table 10 PRESIDENT 5000
20 ANALYST 3000 for each job, 20 ANALYST 6000
20 ANALYST 3000 grouped by
20 CLERK 1900
20 MANAGER 2975 department”
20 MANAGER 2975
30 SALESMAN 1600
30 CLERK 950
30 MANAGER 2850
30 MANAGER 2850
30 SALESMAN 1250
30 SALESMAN 5600
30 CLERK 950
30 SALESMAN 1500
30 SALESMAN 1250
5-16
15
Using the GROUP BY Clause
on Multiple Columns
SQL> SELECT deptno, job, sum(sal)
2 FROM emp
3 GROUP BY deptno, job;
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
20 ANALYST 6000
20 CLERK 1900
...
9 rows selected.
5-17
16
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.
5-18
10 3
20 5
Any column or ex3p0 6CT list that is not an aggregate function must be in the GROUP BY
ression in the SELE
clause.
17
Illegal Queries
Using Group Functions
• 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;
5-19
DEPTNO AVG(SAL)
10 2916.6667
20 2175
18
Excluding Group Results
EMP
DEPTNO SAL
10 2450
10 5000 5000
10 1300
20 800
20 1100 “maximum DEPTNO MAX(SAL)
20 3000 salary --------- ---------
3000
20 3000 per department 10 5000
20 2975 greater than 20 3000
30 1600 $2900”
30 2850
30 1250
2850
30 950
30 1500
30 1250
5-20
19
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.
SELECT column, group_function
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING group_condition]
[ORDER BY column];
5-21
20
Using the HAVING Clause
DEPTNO MAX(SAL)
10 5000
20 3000
5-22
DEPTNO AVG(SAL)
10 2916.6667
20 2175
21
Using the HAVING Clause
JOB PAYROLL
ANALYST 6000
MANAGER 8275
5-23
22
Nesting Group Functions
Display the maximum average salary.
MAX(AVG(SAL))
2916.6667
5-24
23
Summary
SELECT column, group_function(column)
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING group_condition]
[ORDER BY column];
5-25
Summary
Seven group functions are available in SQL:
• AVG
• COUNT
• MAX
• MIN
• SUM
• STDDEV
• VARIANCE
You can create subgroups by using the GROUP BY clause. Groups can be excluded using the HAVING
clause.
Place the HAVING and GROUP BY clauses after the WHERE clause in a statement. Place the ORDER BY
clause last.
The Oracle Server evaluates the clauses in the following order:
• If the statement contains a WHERE clause, the server establishes the candidate rows.
• The server identifies the groups specified in the GROUP BY clause.
• The HAVING clause further restricts result groups that do not meet the group criteria in the HAVING
clause.
24
Practice Overview
5-26
Practice Overview
At the end of this practice, you should be familiar with using group functions and selecting groups of data.
Paper-Based Questions
For questions 1–3, circle either True or False.
Note: Column aliases are used for the queries.
25