5 Reporting Aggregated Data Using the Group Functions Objectives After completing this lesson, you should be able to do the following: Identify the available group functions Describe the use of group functions Group data by using the GROUP BY clause Include or exclude grouped rows by using the HAVING clause 5-2 1
Lesson Agenda Group functions: Types and syntax Use AVG, SUM, MIN, MAX, COUNT Use DISTINCT keyword within group functions NULL values in a group function Grouping rows: GROUP BY clause HAVING clause Nesting group functions 5-3 What Are Group Functions? Group functions operate on sets of rows to give one result per group. EMPLOYEES Maximum salary in EMPLOYEES table 5-4 2
Types of Group Functions AVG COUNT MAX MIN STDDEV SUM VARIANCE Group functions 5-5 Group Functions: Syntax SELECT group_function(column),... FROM table [WHERE condition] [ORDER BY column]; 5-6 3
Using the AVG and SUM Functions You can use AVG and SUM for numeric data. SELECT AVG(salary), MAX(salary), MIN(salary), SUM(salary) WHERE job_id LIKE '%REP%'; 5-7 Using the MIN and MAX Functions You can use MIN and MAX for numeric, character, and date data types. SELECT MIN(hire_date), MAX(hire_date) ; 5-8 4
Using the COUNT Function COUNT(*) returns the number of rows in a table: 1 SELECT COUNT(*) WHERE department_id = 50; COUNT(expr) returns the number of rows with non-null values for expr: 2 SELECT COUNT(commission_pct) WHERE department_id = 80; 5-9 Using the DISTINCT Keyword COUNT(DISTINCT expr) returns the number of distinct non-null values of expr. To display the number of distinct department values in the EMPLOYEES table: SELECT COUNT(DISTINCT department_id) ; 5-10 5
Group Functions and Null Values Group functions ignore null values in the column: 1 SELECT AVG(commission_pct) ; The NVL function forces group functions to include null values: 2 SELECT AVG(NVL(commission_pct, 0)) ; 5-11 Lesson Agenda Group functions: Types and syntax Use AVG, SUM, MIN, MAX, COUNT Use DISTINCT keyword within group functions NULL values in a group function Grouping rows: GROUP BY clause HAVING clause Nesting group functions 5-12 6
Creating Groups of Data EMPLOYEES 4400 9500 Average salary in EMPLOYEES table for each department 3500 6400 10033 5-13 Creating Groups of Data: GROUP BY Clause Syntax SELECT column, group_function(column) FROM table [WHERE condition] [GROUP BY group_by_expression] [ORDER BY column]; You can divide rows in a table into smaller groups by using the GROUP BY clause. 5-14 7
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 department_id, AVG(salary) GROUP BY department_id ; 5-15 Using the GROUP BY Clause The GROUP BY column does not have to be in the SELECT list. SELECT AVG(salary) GROUP BY department_id ; 5-16 8
Grouping by More than One Column EMPLOYEES Add the salaries in the EMPLOYEES table for each job, grouped by department. 5-17 Using the GROUP BY Clause on Multiple Columns SELECT department_id, job_id, SUM(salary) WHERE department_id > 40 GROUP BY department_id, job_id ORDER BY department_id; 5-18 9
Illegal Queries Using Group Functions Any column or expression in the SELECT list that is not an aggregate function must be in the GROUP BY clause: SELECT department_id, COUNT(last_name) ; A GROUP BY clause must be added to count the last names for each department_id. SELECT department_id, job_id, COUNT(last_name) GROUP BY department_id; Either add job_id in the GROUP BY or remove the job_id column from the SELECT list. 5-19 Illegal Queries Using Group Functions 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. SELECT department_id, AVG(salary) WHERE AVG(salary) > 8000 GROUP BY department_id; Cannot use the WHERE clause to restrict groups 5-20 10
Restricting Group Results EMPLOYEES The maximum salary per department when it is greater than $10,000 5-21 Restricting Group Results with the HAVING Clause When you use the HAVING clause, the Oracle server restricts groups as follows: 1. Rows are grouped. 2. The group function is applied. 3. Groups matching the HAVING clause are displayed. SELECT column, group_function FROM table [WHERE condition] [GROUP BY group_by_expression] [HAVING group_condition] [ORDER BY column]; 5-22 11
Using the HAVING Clause SELECT department_id, MAX(salary) GROUP BY department_id HAVING MAX(salary)>10000 ; 5-23 Using the HAVING Clause SELECT job_id, SUM(salary) PAYROLL WHERE job_id NOT LIKE '%REP%' GROUP BY job_id HAVING SUM(salary) > 13000 ORDER BY SUM(salary); 5-24 12
Lesson Agenda Group functions: Types and syntax Use AVG, SUM, MIN, MAX, COUNT Use DISTINCT keyword within group functions NULL values in a group function Grouping rows: GROUP BY clause HAVING clause Nesting group functions 5-25 Nesting Group Functions Display the maximum average salary: SELECT MAX(AVG(salary)) GROUP BY department_id; 5-26 13
Quiz Identify the guidelines for group functions and the GROUP BY clause. 1. You cannot use a column alias in the GROUP BY clause. 2. The GROUP BY column must be in the SELECT clause. 3. By using a WHERE clause, you can exclude rows before dividing them into groups. 4. The GROUP BY clause groups rows and ensures order of the result set. 5. If you include a group function in a SELECT clause, you cannot select individual results as well. 5-27 Summary In this lesson, you should have learned how to: Use the group functions COUNT, MAX, MIN, SUM, and AVG Write queries that use the GROUP BY clause Write queries that use the HAVING clause SELECT column, group_function FROM table [WHERE condition] [GROUP BY group_by_expression] [HAVING group_condition] [ORDER BY column]; 5-28 14
Practice 5: Overview This practice covers the following topics: Writing queries that use the group functions Grouping by rows to achieve more than one result Restricting groups by using the HAVING clause 5-29 15