At A Glance: Group Functions
At A Glance: Group Functions
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:
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
Grouping Data
To specify that groups should be created, add the GROUP BY clause to the SELECT statement.
Oracle 12c: SQL 11-4
• 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.
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.
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
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
3. The STDDEV and VARIANCE functions can be used with what type of data?
ANSWER: Numeric data
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.