Professional Documents
Culture Documents
Grouping Aggregated Data Using Group Function: Module of Instruction
Grouping Aggregated Data Using Group Function: Module of Instruction
Group Function helps how to group rows in a table into smaller sets and how
Multiple-
to specify search criteria for groups of rows.
Row
Function After completing this lesson, the student should be able to:
1. Identify the available group functions
2. Describe the use of group functions
3. Group data by using the GROUP BY clause
4. Use the HAVING clause to include or exclude group function.
Group Function
Group functions are used to operate on sets of rows in order to give one
result per group. These sets may comprise either the entire table or a
table may be split into groups.
_____________________________________________________________________________________
2
MODULE OF INSTRUCTION
Explanation:
The sample output displays the average and sum YR_PUB of all authors.
Explanation:
The sample output displays the authors LNAME and FNAME that is
first in the alphabet lists for MIN function and last in the alphabet lists
for MAX function.
Explanation:
The sample output displays the author’s standard deviation for STDDEV
function and variance for VARIANCE function applied in ID column.
_____________________________________________________________________________________
4
MODULE OF INSTRUCTION
Explanation:
The sample output displays the total number of authors in the table.
Example: Using Count(expr)
SELECT COUNT(TYPE)
FROM AUTHORS
WHERE TYPE='CSIT';
Output:
Explanation:
The sample output displays the total number of authors whose book
TYPE is CSIT.
Explanation:
The sample output displays the number of distinct value that are in
authors table using the YR_PUB column.
Explanation:
The output for default function displays the calculated average based
only on those rows with values in the table in which a valid value is
stored in the YR-PUB column. The average is calculated as the total
YR_PUB to all authors divided by the number of authors with valid
values in YR_PUB column which is equal to 7.
The output for function with NVL displays the calculated average
regardless of whether null values are stored in the YR-PUB column. The
average is calculated as the total YR_PUB to all authors divided by the
_____________________________________________________________________________________
6
MODULE OF INSTRUCTION
number of authors with or without valid values in YR_PUB column
which is equal to 8 (including author with NULL YR_PUB).
Explanation:
The sample select the authors book TYPE and count the number of book
TYPE, the group by clause specifies that the values retrieved should be
group per book TYPE and count the type per group. This only means
that there are 4 types of CSIT, 2 for GENED and 2 for BA.
Example: Violation
Output:
In the example sing TYPE column has no function enforce while
COUNT(*) is a function this will cause an error, the reason why column
with no function should be called in GROUP BY Clause.
_____________________________________________________________________________________
8
MODULE OF INSTRUCTION
Grouping two (2) columns
To see the results for groups within group use this example.
Example:
SELECT TYPE, YR_PUB, SUM(YR_PUB), AVG(YR_PUB)
FROM AUTHORS
GROUP BY TYPE, YR_PUB;
Output:
Explanation:
The sample displays the authors book TYPE, YR_PUB and get the SUM
and AVG for YR_PUB column then group the data by TYPE and
YR_PUB, since the YR_PUB have more distinct values the select
statement then prioritize the columns with the most number of distinct
values.
You can always use the WHERE condition to restrict the rows returned
by the query.
Example: Group by with WHERE condition
SELECTTYPE, YR_PUB,
COUNT(DISTINCT(YR_PUB)),SUM(YR_PUB), AVG(YR_PUB)
FROM AUTHORS
WHERE YR_PUB > 1995
GROUP BY TYPE, YR_PUB;
Output:
Explanation:
The sample displays the authors book TYPE, YR_PUB and gets the
number of distinct values, SUM and AVG for YR_PUB column where
YR_PUB is greater than 1995, then group the data by TYPE and
YR_PUB.
Having Clause
• You cannot use the WHERE clause to restrict groups.
• You use the HAVING clause to restrict groups.
• You cannot use group functions in the WHERE clause.
• Syntax
SELECT function_name(column), group_function(column)
FROM table_name
[WHERE condition]
[GROUP BY group_by_expr]
[HAVING group Condition]
[ORDER BY column];
• Example: Illegal Queries
_____________________________________________________________________________________
10
MODULE OF INSTRUCTION
Explanation:
Sample shows that a WHERE condition cannot have a group function in
it ex: WHERE MAX(ID)>15 to restrict the rows return pure column can
only be applied to WHERE condition. By the time that you wanted to
restricts the rows with group function.
Example: with HAVING CLAUSE
SELECT YR_PUB,
COUNT(DISTINCT(YR_PUB)),SUM(YR_PUB), AVG(YR_PUB)
FROM AUTHORS
GROUP BY YR_PUB
HAVING MAX(ID)>15;
Output:
Explanation:
The sample displays the YR_PUB, the distinct values for YR_PUB,
SUM and AVG then group the data per YR_PUB and restricts the rows
retuned having maximum ID greater than 5;
Complete SELECT
Example:
SELECT TYPE, MIN(FNAME),MAX(ID)
FROM AUTHORS
WHERE ID BETWEEN 10 AND 15
GROUP BY TYPE
HAVING MAX(ID)>= 5
ORDER BY MAX(ID) DESC;
Output:
Explanation:
The sample displays the authors book TYPE, number of distinct values
for YR_PUB, SUM and AVG for YR_PUB column whose ID is in
between the lowest and highest range of 10-15 then group the data per
YR_PUB having maximum ID which is greater than or equal to 15 and
sort the data by maximum ID in descending order.
Explanation:
The sample output displays the maximum average YR_PUB of authors
per book TYPE.
_____________________________________________________________________________________
12
MODULE OF INSTRUCTION
Lesson Summary:
In this lesson, you should have learned how to:
• Use the group functions COUNT, MAX, MIN, SUM, and
AVG
• Include the NULL values using the NVL function
• Write queries that use the GROUP BY clause
• Write queries that use the HAVING clause
• Group date using 2 columns
• And apply Nested Groups
Terms to Remember!
AVG - Average value of N ignoring null values
COUNT - Count the number of rows where expr evaluates to something
other than null
COUNT(DISTINCT expr) – While COUNT(DISTINCT expr) returns
the number of unique, non-null values that are in the column identified
by expr.
COUNT(expr) - In contrast, COUNT(expr) returns the number of non-
null values that are in the column identified by expr.
COUNT(*) - COUNT(*) returns the number of rows in a table that
satisfy the criteria of the SELECT statement, including duplicate rows
and rows containing null values in any of the columns.
Group functions - operate on sets of rows to give one result per group.
MAX - Maximum value of expression ignoring null values
MIN - Minimum value of expression ignoring null values
STDDEV - Standard deviation of N ignoring null values
SUM - Sum values of N ignoring null values
VARIANCE - Variance - of N ignoring null values
Textbook:
References Oracle Press (2010). Applied Oracle Security
References:
Pratt, Philip J. (2010). Database management systems
Rob, Peter & Coronel, Carlo (2009). Database Management Systems
Schwalbe, Kathy (2011). Management of Information Technology
Projects
Wheeler, Evan (2011). Security Risk Management : Building an
Information Security Risk Management Program from the Ground Up
Supplementary Video
https://www.youtube.com/watch?v=5rx8Q4x4-qI&t=6s
https://www.youtube.com/watch?v=WQe-p2F3Kcg
https://www.youtube.com/watch?v=mFxejM58wyM
Suggested Reading
SQL Tutorial. In ws3schools, Retrieved from
http://www.w3schools.com/sql/default.asp
Database management system. In Encyclopedia Britannica, Retrieved
from http://www.britannica.com/EBchecked/topic/152201/database-
management-system-DBMS.
SQL. In Encyclopedia Britannica, Retrieved from
http://www.britannica.com/EBchecked/topic/569684/SQL
Database Administration. In Encyclopedia.com, Retrieved from
http://www.encyclopedia.com/topic/Database_administration.aspx
SQL. In Encyclopedia.com, Retrieved from
http://www.encyclopedia.com/topic/SQL.aspx
_____________________________________________________________________________________
14
MODULE OF INSTRUCTION
Tutorialspoint.com
oracle.com
apex.oracle.com