Professional Documents
Culture Documents
Les 05
Les 05
Aggregating Data
Using Group Functions
After
After completing
completing this
this lesson,
lesson, you
you should
should
be
be able
able to
to do
do the
the following:
following:
•• Identify
Identify the
the available
available group
group functions
functions
•• Describe
Describe the
the use
use of
of group
group functions
functions
•• Group
Group data
data using
using the
the GROUP
GROUP BY
BY clause
clause
•• Include
Include or
or exclude
exclude grouped
grouped rows
rows by
by
using
using the
the HAVING
HAVING clause
clause
•• AVG
AVG
•• COUNT
COUNT
•• MAX
MAX
•• MIN
MIN
•• STDDEV
STDDEV
•• SUM
SUM
•• VARIANCE
VARIANCE
MIN(HIRED MAX(HIRED
--------- ---------
17-DEC-80 12-JAN-83
COUNT(*)
---------
6
COUNT(COMM)
-----------
4
AVG(COMM)
---------
550
AVG(NVL(COMM,0))
----------------
157.14286
Divide
Divide rows
rows in
in aa table
table into
into smaller
smaller groups
groups
by
by using
using the
the GROUP
GROUP BY BY clause.
clause.
DEPTNO AVG(SAL)
--------- ---------
10 2916.6667
20 2175
30 1566.6667
AVG(SAL)
---------
2916.6667
2175
1566.6667
SQL>
SQL> SELECT
SELECT deptno,
deptno, COUNT(ename)
COUNT(ename)
BYY c
cllaau
u ssee
22 FROM
FROM emp;
emp;
n t
t RO
h
h OUUPPB
GR O
ee G
m i
iissssinn g
g i
i n
o l u
u m
mnn m
Col
C
SELECT
SELECT deptno,
deptno, COUNT(ename)
COUNT(ename)
**
ERROR
ERROR at
at line
line 1:
1:
ORA-00937:
ORA-00937: not
not aa single-group
single-group group
group function
function
DEPTNO MAX(SAL)
--------- ---------
10 5000
20 3000
JOB PAYROLL
--------- ---------
ANALYST 6000
MANAGER 8275
MAX(AVG(SAL))
-------------
2916.6667
Order
Order of
of evaluation
evaluation of
of the
the clauses:
clauses:
•• WHERE
WHERE clause
clause
•• GROUP
GROUP BY
BY clause
clause
•• HAVING
HAVING clause
clause