This document discusses group functions in SQL, which operate on groups of rows and return results. It covers common group functions like COUNT, AVG, SUM, MIN, MAX, STDDEV and VARIANCE. It explains how to use the GROUP BY clause to group data by columns and the HAVING clause to include or exclude grouped rows. Examples are provided for counting rows, getting averages, and filtering groups. Nested groups and the order of operations are also discussed.
3. OBJECTIVES
At the end of this lesson, you will learn:
How to identify the available group functions
How to describe the use of group functions in
select statements
Grouping data, by using the Group By clause and
How to include or exclude grouped rows, by
using the having clause.
4. WHAT ARE GROUP FUNCTIONS?
Group functions are SQL functions, which operate
on a GROUP of rows, and return a result.
This group of rows may be an entire column or a
column, split into smaller groups.
6. This example uses the GROUP function called COUNT, to
count the number of employees that earn a salary
from the company.
AVG & SUM
The AVG and Sum keyword is used to find the
average and sum –respectively- of a group of
numbers.
7. EXERCISE
Query your database for the average salary of all
the employees in the department with where
department_id=90.
8.
9. MIN & MAX
The MAX and the MIN functions, will display the
maximum and minimum –respectively- of all the
values in the specified group, or column.
EXAMPLE
10. STDDEV & VARIANCE
STDDEV and VARIANCE are used to find the standard
deviation and the variance, of the numbers in the
specified column or group.
COUNT
The COUNT function counts the number of rows in
the stated group or column. It has three different
variations but each performs the same function
of counting the number of rows in the group.
11. COUNT (COLUMN_NAME)
This SQL statement COUNTs all the rows in the
stated column as returns their total number.
FORMAT:
SELECT COUNT (column_name)
FROM table_name;
15. The SQL statement counts the number of rows in
the manager _id column.
COUNT DISTINCT (COLUMN_NAME)
Where the same value occurs many times in a
column, the Oracle server COUNTS them all as one
value when the SELECT COUNT keyword is used.
16.
17. The Oracle server would count the different
values in the manager_id column instead of the
number of rows.
From the result of this query, it is obvious, that all
the 107 employees, share only 18 manager-ids
18. The DISTINCT keyword, when used with a group
function, will specify only the different rows
available in that group.
The opposite of the DISTINCT keyword, is the ALL
keyword, and it operates on all the rows in the
group, including duplicates. Only null values are
exempted.
19. GROUPING DATA
Initially we said group functions are functions
that operate on a group of rows.
We also said these group of rows could be an
entire column.
When we want to specify the order in which the
output of a GROUP function is processed, we use the
GROUP BY keyword.
21. The Oracle server goes to the department_id
column and fetches the distinct departments.
Afterwards, the Oracle server comes to the salary
column and begins to group the rows in the
salary column by their various departments, and
then finally, for each different department_id, the
Oracle server returns the minimum salary.
22. When using the GROUP BY clause, one general rule is
that GROUP BY is always followed by a column
name.
No column alias can be used with the GROUP BY clause.
EXAMPLE:
24. In the event of Nested GROUPS, the inner GROUP function
is SELECT before the outer GROUP function.
INCLUDING & EXCLUDING ROWS
When restricting rows in a SELECT clause, we use the
WHERE keyword. However, when restricting rows in
a GROUP BY clause, we use the HAVING keyword.
The GROUP BY clause will only return the rows that
meet the HAVING condition.