Chapter 7
Multiple-Row Function and
Group By clause
Objectives
At the end of this lesson, you should be
able to:
Identify
the available group functions
Describe the use of group functions
Use the GROUP BY clause to group data
Multiple-row Functions Definition
Multiple-row functions are functions that
return a result based on multiple-row.
It is also known as Aggregate function or a
group function.
Group functions operate on sets of rows to
give one result per group.
Multiple-row Functions Definition
GPA
3.21
2.71
3.82
Maximum GPA in
the STUDENT table
3.05
1.89
3.89
2.22
3.11
3.34
1.88
3
1.88
2.74
3.12
Highest GPA
3.89
Types of Multiple-row Functions
Function
Description
Max (expr)
Maximum value in a column, ignore null values
Min (expr)
Minimum value in a column, ignore null values
Avg (expr)
Average of all values in a column, ignore null
values
Sum (expr)
Sum of all the values in a column, ignore null
values
Count (expr)
Number of rows in the column that are not null.
Count (*)
Total number of rows in the table, including
duplicates and rows with nulls.
Using Multiple-row Functions
Max,
min and count functions can be use on text,
number and date columns.
Sum and avg functions only can be applied on
numeric columns only.
All group functions except Count(*) ignore null
values. To substitute a value for null values, use the
NZ function.
Using the MAX and MIN
Functions
SELECT Max(GPA) AS [Highest GPA],
Max (LastName) AS [First in Alpha order] ,
Min(LastName) As [Last in Alpha Order] ,
Min(DateEnrolled) As [Earliest Enrolled Date]
from Student;
Output:
Highest
GPA
First
in Alpha order
AVG(SAL)
SUM(SAL)
---------------3.89 Williams
1400
5600
Last in Alpha Order
Bartell
Earliest Enrolled Date
3/3/2000
Using the AVG and SUM Functions
SELECT AVG(GPA) AS [Average], SUM(GPA) As [Total GPA]
from Student;
Output:
Average
2.84714285714286
Total GPA
39.86
Using the COUNT Function
COUNT(*)
returns the number of rows in a
query.
SELECT Count(*) AS [No of Students]
FROM Student;
Output:
No of Students
16
The example above will calculate the number of rows from the STUDENT table.
Using the COUNT Function
COUNT(expr)
returns the number of
nonnull rows.
SELECT Count(GPA) As [Earn GPA]
FROM Student;
Output:
Earn GPA
14
The example above calculate the number of rows from the GPA columns
that are not null.
Group Functions and Null
Values
Group
functions ignore null values in the
column.
SELECT AVG(GPA) AS [Average GPA]
FROM Student;
Output:
Average GPA
2.84714285714286
* The AVG divides the total GPA by 14 instead of 16 students.
The
Using the NZ Function
with Group Functions
NZ function forces group functions to
include null values.
SELECT AVG(NZ(GPA,0)) AS [Average GPA]
FROM Student;
Output:
Average GPA
2.49125
The average is calculated as total GPA being divide by all students
in the STUDENT table, 16 students.
Creating Groups of Data
CourseID
GPA
DCS
3.21
DCS
1.89
DCS
3.89
DCS
2.74
DIC
3.34
DIC
2.22
DCS
3.89
DIC
3.34
DIC
3.34
DIC
3.12
DICT
3.11
DIT
3.82
DICT
DICT
3.11
DICT
DIT
3.82
DIT
3.05
DIT
1.88
DIT
1.88
DNC
Max GPA for each course in
STUDENT table
CourseID
DNC
Highest GPA
Creating Groups of Data:
GROUP BY Clause
Use
the GROUP BY clause to divide rows
in a table into smaller groups.
SELECT
FROM
[WHERE
[GROUP BY
[ORDER BY
column, group_function
table
condition]
group_by_expression]
column];
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 CourseID, Max(GPA) As [Highest GPA]
FROM Student
Group By CourseID;
Output:
CourseID
Highest GPA
DCS
3.89
DIC
3.34
DICT
3.11
DIT
3.82
DNC
Using the GROUP BY Clause
The
GROUP BY column does not have to
be in the SELECT list.
SELECT Max(GPA) As [Highest GPA]
FROM Student
Group By CourseID;
Output:
Highest GPA
3.89
3.34
3.11
3.82
Using the GROUP BY Clause
Display
the number of students in each
course.
SELECT CourseID, Count(*) As [Course Students]
FROM Student
Group By CourseID;
Output:
CourseID
Course Students
DCS
DIC
DICT
DIT
DNC
Using a Group Function in the
ORDER BY Clause
SELECT CourseID, Count(*) As [Course Students]
FROM Student
Group By CourseID
Order By Count(*);
Output:
CourseID
Course Students
DNC
DICT
DIT
DIC
DCS
Summary
Showing
different queries that use group
functions
Grouping by rows to achieve more than
one result