Group Functions
Group Functions
Systems
Subject Teacher: Zartasha Baloch
2
Group Functions
Lecture # 16
Disclaimer: The material used in this presentation to deliver the lecture i.e., definitions/text and
pictures/graphs etc. does not solely belong to the author/presenter. The presenter has gathered this
lecture material from various sources on web/textbooks. Following sources are especially
acknowledged:
1. Connolly, Thomas M., and Carolyn E. Begg. Database systems: a practical approach to design, implementation,
and management. Pearson Education, 2005.
2. Gorman, Tim, Inger Jorgensen, Melanie Caffrey, and Lex deHaan. Beginning Oracle SQL: For Oracle Database
12c. Apress, 2014.
3. Greenberg, Nancy, and Instructor Guide PriyaNathan. "Introduction to Oracle9i: SQL." ORACLE, USA (2001).
Objectives
EMPLOYEES
Group functions operate on
sets of rows to give one result
per group.
The
maximum
salary in
the
EMPLOYEES
table.
…
Types of Group Functions
AVG
COUNT
MAX
MIN
STDDEV
SUM
VARIANCE
Group Functions Syntax
You can use MIN and MAX for any data type.
SELECT COUNT(*)
FROM employees
WHERE department_id = 50;
Using the COUNT Function
SELECT COUNT(commission_pct)
FROM employees
WHERE department_id = 80;
Using the DISTINCT Keyword
SELECT AVG(commission_pct)
FROM employees;
Using the NVL Function
with Group Functions
EMPLOYEES
4400
9500
The
3500 average
salary
in
EMPLOYEES
6400 table
for each
department.
10033
…
Creating Groups of Data:
The GROUP BY Clause Syntax
SELECT AVG(salary)
FROM employees
GROUP BY department_id ;
Grouping by More Than One Column
EMPLOYEES
“Add up the
salaries in
the EMPLOYEES
table
for each job,
grouped by
department.
…
Using the GROUP BY Clause
on Multiple Columns
SELECT department_id dept_id, job_id, SUM(salary)
FROM employees
GROUP BY department_id, job_id ;
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.
The maximum
salary
per department
when it is
greater than
$10,000
…
Excluding Group Results: The HAVING
Clause
SELECT MAX(AVG(salary))
FROM employees
GROUP BY department_id;
Summary