0% found this document useful (0 votes)
168 views25 pages

Day-2 Aggregate Functions

This document discusses group functions in SQL, which operate on sets of rows to return a single result per group. It covers the main group functions like COUNT, MAX, MIN, AVG, and SUM and how to use the GROUP BY clause to divide rows into groups and the HAVING clause to filter groups. It provides examples of finding minimum/maximum salaries per department, average salary by job, and using nested group functions. The key points are that columns in the SELECT list not in a group function must be in the GROUP BY, and the HAVING clause is used to filter groups after they are created rather than the WHERE clause.

Uploaded by

Ashok Durai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
168 views25 pages

Day-2 Aggregate Functions

This document discusses group functions in SQL, which operate on sets of rows to return a single result per group. It covers the main group functions like COUNT, MAX, MIN, AVG, and SUM and how to use the GROUP BY clause to divide rows into groups and the HAVING clause to filter groups. It provides examples of finding minimum/maximum salaries per department, average salary by job, and using nested group functions. The key points are that columns in the SELECT list not in a group function must be in the GROUP BY, and the HAVING clause is used to filter groups after they are created rather than the WHERE clause.

Uploaded by

Ashok Durai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Aggregating Data

Using Group Functions

What Are Group Functions?


Group functions operate on sets of rows to give
one result per group.
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

maximum
salary in
the EMP table

MAX(SAL)
--------5000

Types of Group Functions


AVG ([DISTINCT|ALL]n)
COUNT ({ *|[DISTINCT|ALL]expr})
MAX ([DISTINCT|ALL]expr)
MIN ([DISTINCT|ALL]expr)
STDDEV ([DISTINCT|ALL]x)
SUM ([DISTINCT|ALL]n)
VARIANCE ([DISTINCT|ALL]x)

Using AVG and SUM


Functions
You can use AVG and SUM for numeric data.
SQL> SELECT
2
3 FROM
4 WHERE

AVG(sal), MAX(sal),
MIN(sal), SUM(sal)
emp
job LIKE 'SALES%';

AVG(SAL) MAX(SAL) MIN(SAL) SUM(SAL)


-------- --------- --------- --------1400
1600
1250
5600

Using MIN and MAX


Functions
You can use MIN and MAX for any datatype.
SQL> SELECT
2 FROM

MIN(hiredate), MAX(hiredate)
emp;

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

Using the COUNT Function


COUNT(*) returns the number of rows
in a table.
SQL> SELECT
2 FROM
3 WHERE
COUNT(*)
--------6

COUNT(*)
emp
deptno = 30;

Using the COUNT Function


COUNT(expr) returns the number of
nonnull rows.
SQL> SELECT
2 FROM
3 WHERE
COUNT(COMM)
----------4

COUNT(comm)
emp
deptno = 30;

Group Functions and Null


Values

Group functions ignore null values


in the column.
SQL> SELECT AVG(comm)
2 FROM
emp;

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

Using the NVL Function


with Group Functions
The NVL function forces group
functions to include null values.
SQL> SELECT AVG(NVL(comm,0))
2 FROM
emp;

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

Creating Groups of Data


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

2916.6667

average
salary
2175 in EMP
table
for each
department
1566.6667

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

Creating Groups of Data:


GROUP BY Clause
SELECT
FROM
[WHERE
[GROUP BY
[ORDER BY

column, group_function
table
condition]
group_by_expression]
column];

Divide rows in a table into smaller


groups by using the GROUP BY clause.

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

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

Grouping by More
Than One Column
EMP
DEPTNO
--------10
10
10
20
20
20
20
20
30
30
30
30
30
30

JOB
SAL
--------- --------MANAGER
2450
PRESIDENT
5000
CLERK
1300
CLERK
800
CLERK
1100
ANALYST
3000
ANALYST
3000
MANAGER
2975
SALESMAN
1600
MANAGER
2850
SALESMAN
1250
CLERK
950
SALESMAN
1500
SALESMAN
1250

sum salaries in
the EMP table
for each job,
grouped by
department

DEPTNO
-------10
10
10
20
20
20
30
30
30

JOB
SUM(SAL)
--------- --------CLERK
1300
MANAGER
2450
PRESIDENT
5000
ANALYST
6000
CLERK
1900
MANAGER
2975
CLERK
950
MANAGER
2850
SALESMAN
5600

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.

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.
e
s
u
cla

Y
B
SQL>
SQL> SELECT
SELECT deptno,
deptno, COUNT(ename)
COUNT(ename) P
U
22 FROM
emp;
FROM
emp;
O
R
G
he
t
SELECT
SELECT deptno,
deptno, COUNT(ename)
COUNT(ename)
in
g
**
in
s
ERROR
ERROR at
at line
line 1:
1: is
m
ORA-00937:
single-group
n
ORA-00937: not
not aa
single-group group
group function
function
m
lu
o
C

Illegal Queries
Using Group Functions
You cannot use the WHERE clause to restrict
groups.
You use the HAVING clause to restrict groups.
SQL>
SQL>
22
33
44

SELECT
SELECT
FROM
FROM
WHERE
WHERE
GROUP
GROUP BY
BY

deptno,
deptno, AVG(sal)
AVG(sal)
e
s
emp
u
emp
a
l
AVG(sal)
c
AVG(sal) >> 2000
2000
E
deptno;
deptno;
ER s

H up s
W ro
e
WHERE
h ict g
WHERE AVG(sal)
AVG(sal) >> 2000
2000
t
e str
**
s
u re
ERROR
t
ERROR at
at line
line 3:
3:
o to
n
ORA-00934:
group
function
ORA-00934: groupanfunction is
is not
not allowed
allowed here
here
C

Excluding Group Results


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

3000

2850

maximum
salary
per department
greater than
$2900

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

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
FROM
[WHERE
[GROUP BY
[HAVING
[ORDER BY

column, group_function
table
condition]
group_by_expression]
group_condition]
column];

Using the HAVING Clause


SQL>
2
3
4

SELECT
FROM
GROUP BY
HAVING

deptno, max(sal)
emp
deptno
max(sal)>2900;

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

Using the HAVING Clause


SQL>
2
3
3
4
5

SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY

job, SUM(sal) PAYROLL


emp
job NOT LIKE 'SALES%'
job
SUM(sal)>5000
SUM(sal);

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

Nesting Group Functions


Display the maximum average salary.
SQL> SELECT
max(avg(sal))
2 FROM
emp
3 GROUP BY deptno;
MAX(AVG(SAL))
------------2916.6667

Summary
SELECT
FROM
[WHERE
[GROUP BY
[HAVING
[ORDER BY

column, group_function
table
condition]
group_by_expression]
group_condition]
column];

5. Modify p5q4.sql to display the minimum, maximum, sum,


and average salary for each job type.
Resave to a file called p5q5.sql. Rerun your query.
JOB
Maximum Minimum
Sum Average
---------- -------- -------- ------- -------ANALYST
3000
3000
6000
3000
CLERK
1300
800
4150
1038
MANAGER
2975
2450
8275
2758
PRESIDENT
5000
5000
5000
5000
SALESMAN
1600
1250
5600
1400
6. Write a query to display the number of people with the
same job.
JOB

COUNT(*)

---------- -------ANALYST
2
CLERK
MANAGER
3
PRESIDENT
1

9. Display the manager number and the salary of the lowest


paid employee for that manager.
Exclude anyone where the manager id is not known.
Exclude any groups where the minimum
salary is less than $1000. Sort the output in descending
order of salary.
MGR
--------7566
7839
7782
7788

MIN(SAL)
-------3000
2450
1300
1100

10. Write a query to display the department name, location


name, number of employees, and the
average salary for all employees in that department. Label
the columns dname, loc,
Number of People, and Salary, respectively.
DNAME
LOC
Number of People Salary
------------ --------- ---------------- ------ACCOUNTING
NEW YORK
3 2916.67

You might also like