Schedule: Timing Topic: 35 Minutes 40 Minutes Practice 75 Minutes Total
Schedule: Timing Topic: 35 Minutes 40 Minutes Practice 75 Minutes Total
35 minutes Lecture
40 minutes Practice
75 minutes Total
Lesson Aim
This lesson further addresses functions. It focuses on obtaining summary information, such as
averages, for groups of rows. It discusses how to group rows in a table into smaller sets and how to
specify search criteria for groups of rows.
Instructor Note
Stress the use of DISTINCT and group functions ignoring null values. ALL is the default and is very
rarely specified.
Note: AVG, SUM, VARIANCE, and STDDEV functions can be used only with numeric data types.
Instructor Note
Demo: 5_count1.sql, 5_count2.sql
Instructor Note
Group results are sorted implicitly, on the grouping column. You can use ORDER BY to specify a
different sort order, remembering to use only group functions, or the grouping column.
Instructor Note
Demonstrate the query with and without the DEPARTMENT_ID column in the SELECT statement.
Introduction to Oracle9i: SQL 5-16
Groups within Groups
Sometimes you need to see results for groups within groups. The slide shows a report that displays the
total salary being paid to each job title, within each department.
The EMPLOYEES table is grouped first by department number and, within that grouping, by job title.
For example, the four stock clerks in department 50 are grouped together and a single result (total
salary) is produced for all stock clerks within the group.
Instructor Note
Demo: 5_order1.sql, 5_order2.sql
Purpose: To illustrate ordering columns that are grouped by DEPARTMENT_ID first and ordering
columns that are grouped by JOB_ID first.
Any column or expression in the SELECT list that is not an aggregate function must be in the GROUP
BY clause.
Instructor Note
Demo: 5_error.sql
Instructor Note
Demo: 5_job1.sql, 5_job2.sql
Purpose: To illustrate using a WHERE clause to restrict rows by JOB_ID and using a HAVING clause