0% found this document useful (0 votes)
72 views7 pages

At A Glance: Group Functions

This chapter discusses group functions in Oracle SQL. Group functions operate on multiple rows and return one result per group of rows, unlike single-row functions. The chapter covers how to use aggregation functions like SUM, AVG, COUNT, MIN, and MAX. It also explains how to group data using the GROUP BY clause and filter grouped results using the HAVING clause. The chapter discusses nesting functions, statistical group functions like STDDEV and VARIANCE, and enhanced aggregation with grouping sets, cubes, and rollups.

Uploaded by

brd83477
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views7 pages

At A Glance: Group Functions

This chapter discusses group functions in Oracle SQL. Group functions operate on multiple rows and return one result per group of rows, unlike single-row functions. The chapter covers how to use aggregation functions like SUM, AVG, COUNT, MIN, and MAX. It also explains how to group data using the GROUP BY clause and filter grouped results using the HAVING clause. The chapter discusses nesting functions, statistical group functions like STDDEV and VARIANCE, and enhanced aggregation with grouping sets, cubes, and rollups.

Uploaded by

brd83477
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Oracle 12c: SQL 11-1

Chapter 11
Group Functions

At a Glance
Instructor’s Notes
♦ Chapter Overview

♦ Chapter Objectives

♦ Instructor Notes

♦ Troubleshooting Tips

♦ Quick Quizzes

♦ Discussion Questions

♦ Key Terms
Oracle 12c: SQL 11-2

Chapter Overview
Unlike single-row functions, group functions return one result per group of rows processed. Data
can be grouped using the GROUP BY clause. The actual groups displayed in the results can be
specified using the HAVING clause. A HAVING clause serves as a WHERE clause for grouped
data.

Chapter Objectives
After completing this chapter, you should be able to do the following:

♦ Differentiate between single-row and multiple-row functions


♦ Use the SUM and AVG functions for numeric calculations
♦ Use the COUNT function to return the number of records containing non-NULL
values
♦ Use COUNT(*) to include records containing NULL values
♦ Use the MIN and MAX functions with nonnumeric fields
♦ Determine when to use the GROUP BY clause to group data
♦ Explain when the HAVING clause should be used
♦ List the order of precedence for evaluating WHERE, GROUP BY, and HAVING
clauses
♦ State the maximum depth for nesting group functions
♦ Nest a group function inside a single-row function
♦ Calculate the standard deviation and variance of a set of data, using the STDDEV and
VARIANCE functions
♦ Understand the concept of multidimensional analysis
♦ Perform enhanced aggregation grouping with GROUPING SETS, CUBE, and
ROLLUP
♦ Use composite columns and concatenated groupings in grouping operations

Instructor Notes

Group Functions
Group functions affect multiple rows. The SUM and AVG functions apply to numeric data only,
whereas the COUNT, MAX, and MIN functions can be used for any data types. An asterisk is
used as the argument for the COUNT function to include rows containing NULL values. NULL
values are ignored by the other functions.

The GROUP BY clause is used to group data. If an individual column is listed in the SELECT
clause, along with a group function, the column must also be listed in a GROUP BY clause.
Oracle 12c: SQL 11-3

Troubleshooting Tip Highlight the two new clauses being added to the SELECT
statement.

Troubleshooting Tip All group functions except the COUNT(*) function ignore
NULL values. To include NULL values, nest the NVL function
within the group function.

Quick Quiz
1. Which group functions can be used on date values?
ANSWER: COUNT, MAX, and MIN

2. Which group functions can only be used with numeric values?


ANSWER: SUM, AVG, STDDEV, and VARIANCE

3. Which group function will include NULL values?


ANSWER: COUNT(*)

4. When is the GROUP BY clause used?


ANSWER: When data needs to be grouped—required if an individual column is listed in
the SELECT clause along with a group function.

5. Which group functions can be used with character data?


ANSWER: COUNT, MAX, and MIN

Grouping Data
To specify that groups should be created, add the GROUP BY clause to the SELECT statement.
Oracle 12c: SQL 11-4

When using the GROUP BY clause, remember the following:

• If a group function is used in the SELECT clause, then any individual columns
(nonaggregate) listed in the SELECT clause must be listed in the GROUP BY clause.
• Columns used to group data in the GROUP BY clause do not have to be listed in the
SELECT clause. They are included in the SELECT clause only to have the groups
identified in the output.
• Column aliases cannot be used in the GROUP BY clause.
• Results returned from a SELECT statement that include a GROUP BY clause will
present the results in ascending order of the column(s) listed in the GROUP BY clause.
To present the results in a different sort sequence, use the ORDER BY clause.

Restricting Aggregated Output


Since a WHERE clause cannot contain group functions, the HAVING clause is used to restrict
grouped data. When a SELECT statement contains WHERE, GROUP BY, and HAVING clauses
in the same statement, they are executed in order of (1) WHERE (to restrict rows retrieved from
the table) clause, (2) GROUP BY (to group data) clause, and (3) HAVING (to restrict group data
displayed in the output) clause. When a SELECT statement includes a GROUP BY clause and
no ORDER BY clause, the output will be sorted by the column specified by the GROUP BY
clause.

Troubleshooting Tip Demonstrate the error message that will be returned if a required
GROUP BY clause is not included in a SELECT statement. Then
demonstrate inappropriate use of the HAVING and WHERE
clauses.

Quick Quiz
1. What is the difference between a WHERE clause and a HAVING clause?
ANSWER: The WHERE clause is used to restrict rows retrieved from a table, whereas
the HAVING clause is used to restrict grouped data that has already been retrieved.

2. When is the HAVING clause needed?


ANSWER: Anytime grouped output needs to be restricted

3. If a SELECT statement contains HAVING, GROUP BY, and WHERE clauses, in which
order are they evaluated?
ANSWER: WHERE, GROUP BY, HAVING
Oracle 12c: SQL 11-5

Nesting Functions
Group functions can be nested inside other group functions or single-row functions. Single-row
functions can also be nested inside group functions. However, unlike single-row functions, group
functions can only be nested to a depth of two. When functions are nested, the inner function is
always resolved first.

Troubleshooting Tip Demonstrate that group functions can only be nested to two levels.

Quick Quiz
1. Can a group function be nested inside a single row function?
ANSWER: Yes

2. Can a group function be nested inside another group function?


ANSWER: Yes

3. Can a single-row function be nested inside a group function?


ANSWER: Yes

4. To what maximum depth can group functions be nested?


ANSWER: 2

5. In what order are nested functions evaluated?


ANSWER: The inner function is evaluated first and then the outer function.

Statistical Group Functions


The statistical functions STDDEV and VARIANCE are based on a normal distribution. With a
normal distribution, it is assumed that 68% of the sample is within one standard deviation of the
mean, and 95% is within two standard deviations. The average student who has not had previous
exposure to statistical calculations will have difficulty understanding the material in this section.

Quick Quiz
1. What is the purpose of the STDDEV function?
ANSWER: Used to calculate the standard deviation of a set of data—how close the data
values are to the average or mean value for the data set

2. What is the purpose of the VARIANCE function?


ANSWER: To determine how wide the data range is
Oracle 12c: SQL 11-6

3. The STDDEV and VARIANCE functions can be used with what type of data?
ANSWER: Numeric data

Enhanced Aggregation for Reporting


Extensions to the GROUP BY clause are provided to achieve aggregated results across multiple
dimensions or increasing levels of aggregation in one dimension. This functionality is quite
popular in OLAP and data warehousing development. The grouping sets, cube, and rollup
extensions of the GROUP BY are addressed in this section.

Troubleshooting Tip Introduce the concepts involved via a demonstration of an Excel


spreadsheet Pivot table.

Troubleshooting Tip Oracle 12c introduces new pattern matching features targeted to
analyze patterns or trends of a specific column of data across
many rows. Pattern matching is an advanced topic, however,
students should be made aware of this type of capability.

Discussion Questions
1. Why does management prefer to view grouped or summarized data rather than individual
rows?

2. If individual columns were allowed in the SELECT statements along with group
functions without requiring a GROUP BY clause, what would the output look like?

Key Terms
dimension — Term used to describe any category used in analyzing data, such as time,
geography, and product line.

group functions — Process groups of rows, returning only one result per group of rows
processed. Also called multiple-row functions and aggregate functions.

normal distribution — When a large number of data values are obtained for statistical analysis,
they tend to cluster around some “average” value. This dispersion of values is called normal
distribution.
Oracle 12c: SQL 11-7

standard deviation — A calculation used to determine how closely individual values are to the
mean, or average, of a group of numbers.

statistical group functions — Perform basic statistical calculations for data analysis. Oracle
12c's functions include standard deviation and variance.

You might also like