- •Objectivesj i
- •Creatingi Groups of Data:
- •What Are Group Functions?i
- •Usingi AVG and SUM Functionsi
- •Usingi MININ and MAX Functionsi
- •Usingi the COUNT Functioni
- •Usingi the COUNT Functioni
- •Group Functionsi and Nullll Valuesl
- •Usingi the NVL Functioni
- •Creatingi Groups of Data
- •Usingi the GROUP BY Clausel
- •Usingi the GROUP BY Clausel
- •Usingi the GROUP BY Clausel
- •IllegalIll l Queriesi
- •IllegalIll l Queriesi
- •Excludingl i Group Resultsl
- •Excludingl i Group Results:l
- •Usingi the HAVINGI Clausel
- •Usingi the HAVINGI Clausel
- •Nestingi Group Functionsi
- •Practicei Overviewi
Usingi the GROUP BY Clausel
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 |
3GROUP BY deptno;
|
|
|
|
|
DEPTNO |
AVG(SAL) |
|
|
--------- --------- |
|
|
|
10 |
2916.6667 |
|
|
20 |
2175 |
|
|
30 |
1566.6667 |
|
|
|
|
|
5-14
Usingi the GROUP BY Clausel
The GROUP BY column does not have to be in the SELECT list.
SQL> |
SELECT |
AVG(sal) |
2 |
FROM |
emp |
3GROUP BY deptno;
AVG(SAL)
---------
2916.6667
2175
1566.6667
5-15
EMP
Groupingi by More Than One Columnl
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DEPTNO |
JOB |
SUM(SAL) |
|
|
|
-------- |
--------- --------- |
|
|
“sum salaries in |
|
10 |
CLERK |
1300 |
|
|
10 |
MANAGER |
2450 |
|
|
the EMP table |
|
10 |
PRESIDENT |
5000 |
|
for each job, |
|
20 |
ANALYST |
6000 |
|
grouped by |
|
|
|||
|
20 |
CLERK |
1900 |
|
|
department” |
|
|
|||
|
20 |
MANAGER |
2975 |
|
|
|
|
|
|||
|
|
30 |
CLERK |
950 |
|
|
|
30 |
MANAGER |
2850 |
|
|
|
30 |
SALESMAN |
5600 |
|
|
|
|
|
|
|
|
|
|
|
|
|
5-16
Usingi the GROUP BY Clausel
on Multiplel i le Columnsl
SQL> |
SELECT |
deptno, job, sum(sal) |
2 |
FROM |
emp |
3GROUP 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.
5-17
IllegalIll l Queriesi
Usingi Group Functionsi
Any column or expression in the SELECT
list that is not an aggregate function must |
|||||||||
be in the GROUP BY clause. |
|
e |
|||||||
|
u |
||||||||
|
|
|
|
|
|
|
|
s |
|
|
|
|
|
|
|
|
|
a |
|
|
|
|
|
|
|
|
|
l |
|
|
|
|
|
|
|
|
|
c |
|
|
|
|
|
|
|
|
|
Y |
|
|
|
|
|
|
|
|
|
B |
|
|
|
SQL> SELECT |
deptno, COUNT(ename) |
P |
|
||||
|
|
|
|
|
|
U |
|
|
|
|
|
2 FROM |
emp; |
|
O |
|
|
||
|
|
|
R |
|
|
|
|||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
G |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
e |
|
|
|
|
|
|
|
|
|
h |
|
|
|
|
|
|
|
|
|
t |
|
|
|
|
|
|
|
|
n |
|
|
|
|
|
|
|
|
|
i |
|
|
|
|
|
|
|
SELECT deptno, COUNT(ename) |
|
|
|
|
|||
|
|
|
|
g |
|
|
|
|
|
|
|
* |
|
n |
|
|
|
|
|
|
|
|
i |
|
|
|
|
|
|
|
|
|
|
s |
|
|
|
|
|
|
|
|
|
s |
|
|
|
|
|
|
|
ERROR at line 1: |
i |
|
|
|
|
|
|
|
|
m |
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
ORA-00937: not na single-group group function |
|
||||||
|
|
|
m |
|
|
|
|
|
|
|
|
|
u |
|
|
|
|
|
|
|
|
|
l |
|
|
|
|
|
|
|
|
o |
|
|
|
|
|
|
|
|
|
C |
|
|
|
|
|
|
|
5-18
IllegalIll l Queriesi
Usingi Group Functionsi
•• You cannott use thet WHERE clausel toto restrictr tri t groupsr ..
•• You use the HAVING clause to restrict groups. |
||||||||||||||||||||
|
|
|
|
t |
|
|
I |
|
|
|
l |
|
|
|
to r tri t r . |
|||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SQL> SELECT |
deptno, AVG(sal) |
|
|
e |
|
|
|
|
|||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s |
|
|
|
|
|
|
2 |
FROM |
emp |
|
|
|
|
|
|
|
|
|
u |
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
a |
|
|
|
|
|||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
l |
|
|
|
|
|
|
3 |
WHERE |
AVG(sal) > 2000 |
|
|
c |
|
|
|
|
|||||||||
|
|
E |
|
|
|
|
||||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||
|
|
4 |
GROUP BY deptno; |
|
|
|
|
|
|
R |
s |
|
|
|
|
|||||
|
|
|
|
|
|
|
E |
|
|
|
|
|
||||||||
|
|
|
|
|
|
|
|
|
|
|
|
H up |
|
|
|
|
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||
|
|
|
|
|
|
|
|
|
|
|
W r |
|
|
|
|
|
||||
|
|
|
|
|
|
|
|
|
|
|
e |
|
|
o |
|
|||||
|
|
|
WHERE AVG(sal) > 2000 |
|
|
|
|
g |
|
|
|
|
|
|
||||||
|
|
|
|
|
th |
|
t |
|
|
|
|
|
|
|
||||||
|
|
|
|
|
|
|
|
e |
c |
|
|
|
|
|
|
|
||||
|
|
|
|
|
|
|
|
i |
|
|
|
|
|
|
|
|||||
|
|
|
|
* |
|
|
|
s |
|
r |
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
t |
|
|
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
u |
|
|
s |
|
|
|
|
|
|
|
|
|
|
|
|
|
ERROR at line 3: |
|
t |
|
|
e |
|
|
|
|
|
|
|
|
|
|||
|
|
|
|
o |
|
r |
|
|
|
|
|
|
|
|
|
|||||
|
|
|
|
|
|
|
o |
|
|
|
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
n t |
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
ORA-00934: groupnfu cti n is not allowed here |
|
|
|||||||||||||||
|
|
|
|
|
a |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
C |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5-19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||
Excludingl i Group Resultsl
EMP
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
5000
“maximum
3000 salary
per department greater than $2900”
2850
DEPTNO MAX(SAL)
--------- ---------
10 5000
20 3000
5-20
Excludingl i Group Results:l
HAVINGI Clausel
Use the HAVING clause to restrict groups
–Rows arere groupedr ..
–The groupr functionf ti isis appliedli ..
–Groupsr matchingt i thet HAVINGI clausel arere displayedi l ..
|
|
|
|
|
SELECT |
column, group_function |
|
|
FROM |
table |
|
|
[WHERE |
condition] |
|
|
[GROUP BY |
group_by_expression] |
|
|
[HAVING |
group_condition] |
|
|
[ORDER BY |
column]; |
|
|
|
|
|
5-21
Usingi the HAVINGI Clausel
SQL> |
SELECT |
deptno, max(sal) |
2 |
FROM |
emp |
3GROUP BY deptno
4 HAVING max(sal)>2900;
|
|
|
|
|
DEPTNO |
MAX(SAL) |
|
|
--------- |
--------- |
|
|
10 |
5000 |
|
|
20 |
3000 |
|
|
|
|
|
|
|
|
|
5-22
Usingi the HAVINGI Clausel
SQL> |
SELECT |
job, SUM(sal) PAYROLL |
2 |
FROM |
emp |
3 |
WHERE |
job NOT LIKE 'SALES%' |
4 |
GROUP BY |
job |
5 |
HAVING |
SUM(sal)>5000 |
6ORDER BY SUM(sal);
JOB PAYROLL
--------- ---------
ANALYST 6000
MANAGER 8275
5-23
