Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

MODULE OF INSTRUCTION

Grouping Aggregated data using


Group Function
Welcome to the 9th module of this course Database Management System 1!
For this lesson, it covers the different type of group function, group the data
using GROUP BY clause and exclude output using HAVING clause.
Sample and demonstration in this lesson uses the AUTHORS table as shown
below:
Table 1.0 Authors

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.

Database Management System 1 1


Week 3 Multiple Row 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.

Types of Group Function


Function Description
AVG Average value of N ignoring null values

COUNT Count the number of rows where expr evaluates to


something other than null
Count all selected rows using *, including duplicates and
rows with null
MAX Maximum value of expression ignoring null values
MIN Minimum value of expression ignoring null values
STDEV Standard deviation of N ignoring null values
SUM Sum values of N ignoring null values
VARIANCE Variance of N ignoring null values

Group Function Syntax:


SELECT group_function(Column_lists),…
FROM table_name
[WHERE condition];
Where
 SELECT Group_function – is the name of the group function ex. MIN
 FROM table_name – is the name of the table
 WHERE condition – is optional

_____________________________________________________________________________________
2
MODULE OF INSTRUCTION

Using the AVG and SUM function


 Can be used only with numeric data types
 Example:
SELECT AVG(YR_PUB), SUM(YR_PUB)
FROM AUTHORS;
 Output:

 Explanation:
The sample output displays the average and sum YR_PUB of all authors.

Using the MIN and MAX function


 You can use the MAX and MIN functions for numeric, character, and
date data types.
 Example:
SELECT MIN(LNAME), MAX(LNAME)
FROM AUTHORS;
 Output:

 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.

Database Management System 1 3


Week 3 Multiple Row Function

Using the STDDEV and VARIANCE function


 Can be used only with numeric data types
 Example:
SELECT STDDEV(ID), VARIANCE(ID)
FROM AUTHORS;
 Output:

 Explanation:
 The sample output displays the author’s standard deviation for STDDEV
function and variance for VARIANCE function applied in ID column.

Using the COUNT function


 The COUNT function has three formats:

1. COUNT(*) - COUNT(*) is use to return 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. If a WHERE clause is included in the SELECT statement,
COUNT(*) returns the number of rows that satisfy the condition in
the WHERE clause.

2. COUNT(expr) - In contrast, COUNT(expr) returns the number of


non-null values that are in the column identified by expr.

3. COUNT(DISTINCT expr) – While COUNT(DISTINCT expr)


returns the number of unique, non-null values that are in the column
identified by expr.

_____________________________________________________________________________________
4
MODULE OF INSTRUCTION

 Example: Using Count(*)


SELECT COUNT(*)
FROM AUTHORS;
 Output:

 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.

 Example: Using Count(DISTINCT expr)


SELECT COUNT(distinct YR_PUB)
FROM AUTHORS;
 Output:

Database Management System 1 5


Week 3 Multiple Row Function

 Explanation:
The sample output displays the number of distinct value that are in
authors table using the YR_PUB column.

Using NVL Function to include NULL values.


 By default group function always ignores NULL values to include the
NULL values in counting of output you may use the NVL function.
 Example: default output
SELECT AVG(YR_PUB)
FROM AUTHORS;
 Output:

 Example: with NVL function


SELECT AVG(NVL(YR_PUB,0))
FROM AUTHORS;
 Output:

 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).

Creating groups of data using GROUP BY clause.


 A group functions output came from the table with large group of
information. In order to divide the table of information into smaller
group a GROUP BY clause is being use.
 Syntax:
SELECT function_name(column), group_function(column)
FROM table_name
[WHERE condition]
[GROUP BY group_by_expr]
[ORDER BY column];
 Where:
group_by_expr - Specifies the columns whose values determine the
basis for grouping rows.

Guidelines in using a GROUP BY clause

• When a group function is included in a SELECT clause, you cannot


select individual column as well, unless the individual column appears in
the GROUP BY clause. You receive an error message if you fail to
include the column list in the GROUP BY clause.
• Using a WHERE clause, you can exclude rows before dividing them into
groups.
• You can substitute column by an Expression in the SELECT statement.
• You must include the columns in the GROUP BY clause.
• You cannot use a column alias in the GROUP BY clause

Database Management System 1 7


Week 3 Multiple Row Function

Using GROUP BY Clause


 If the Select statement contain a GROUP BY clause, make sure that all
columns in the SELECT list that are not group functions are called in the
GROUP BY clause.
 Example:
SELECT TYPE, COUNT(*)
FROM AUTHORS
GROUP BY TYPE;
 Output:

 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;

Database Management System 1 9


Week 3 Multiple Row Function

 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;

Database Management System 1 11


Week 3 Multiple Row Function

 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.

Nesting Group Function


 Group functions can be nested to a depth of two functions.
 Example:
SELECT MAX(AVG(YR_PUB))
FROM AUTHORS
GROUP BY TYPE;
 Output:

 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

Database Management System 1 13


Week 3 Multiple Row Function

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 Reading and Video Link


Supplementary Reading
https://www.tutorialspoint.com/sql_certificate/using_the_group_functions.htm
http://www.dba-oracle.com/t_garmany_7_stor_dba_sum.htm
https://docs.oracle.com/cd/B12037_01/server.101/b10759/functions001.htm

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

Database Management System 1 15

You might also like