SQL - 4 Group Functions F22
SQL - 4 Group Functions F22
Aggregating Data
Using Group Functions
Asif Sohail
University of the Punjab
Punjab University College of Information Technology (PUCIT)
MIN(HIRED MAX(HIRED
--------- ---------
17-DEC-80 12-JAN-83
COUNT(*)
---------
6
COUNT(COMM)
-----------
4
COUNT(DISTINCT deptno)
----------------------
4
AVG(COMM)
---------
550
AVG(NVL(COMM,0))
----------------
157.14286
EMP
DEPTNO SAL
--------- ---------
10 2450
10 5000 2916.6667
10 1300
20 800 “average DEPTNO AVG(SAL)
20 1100 salary
------- ---------
20 3000 2175 in EMP
20 3000 table 10 2916.6667
20 2975 for each 20 2175
30 1600 department” 30 1566.6667
30 2850
30 1250 1566.6667
30 950
30 1500
30 1250
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 h
h GR O
ee G ROOUUPPB
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
SQL>
SQL> SELECT
SELECT deptno,
deptno, AVG(sal)
AVG(sal)
s
see
22 FROM
FROM emp
emp l u
aau
33 WHERE AVG(sal)
AVG(sal) >> 2000 c
c l
WHERE 2000
R EE
R ss
44 GROUP
GROUP BY
BY deptno;
deptno; HEE
H uupp
e W
W r oo
h e
tth ctt g g r
WHERE AVG(sal) > 2000 s
see r
r iic
WHERE AVG(sal) > 2000 u u
tt reess t
t
** n
noo r
ann t
t o
o
ERROR
ERROR at
at line
line 3:Ca
3:C
ORA-00934: group function is not allowed here
ORA-00934: group function is not allowed here
DEPTNO MAX(SAL)
--------- ---------
10 5000
20 3000
JOB PAYROLL
--------- ---------
ANALYST 6000
MANAGER 8275
MAX(AVG(SAL))
-------------
2916.6667
Asif Sohail
Assistant Professor
University of the Punjab
Punjab University College of Information Technology (PUCIT)
Allama Iqbal (Old) Campus, Anarkali
Lahore, Pakistan
Tel: +92-(0)42-111-923-923 Ext. 154
E-mail: asif@pucit.edu.pk