Aggregate Fun
Aggregate Fun
Aggregate Fun
Objectives
– Identify the available group functions
– Describe the use of group functions
– Group data by using the GROUP BY clause
– Include or exclude grouped rows by using the HAVING
clause
What Are Group Functions?
Group functions operate on sets of rows to give one result per
EMPLOYEES
Maximum salary in
EMPLOYEES table
…
Types of Group Functions
– AVG
– COUNT
– MAX
– MIN
Group
– STDDEV functions
– SUM
– VARIANCE
9500
3500 Average
salary in
EMPLOYEES
6400 table for each
department
10033
…
Creating Groups of Data:
GROUP BY Clause Syntax
You can 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.
SELECT AVG(salary)
FROM employees
GROUP BY department_id ;
Grouping by More Than One Column
EMPLOYEES
Add the
salaries in
the EMPLOYEES
table for
each job,
grouped by
department
…
Using the GROUP BY Clause
on Multiple Columns
The maximum
salary
per department
when it is
greater than
$10,000
…
Restricting Group Results
with the HAVING Clause
SELECT MAX(AVG(salary))
FROM employees
GROUP BY department_id;
Summary of Group Functions
In this lesson, you should have learned how to:
– Use the group functions COUNT, MAX, MIN, and AVG
– Write queries that use the GROUP BY clause
– Write queries that use the HAVING clause