Group Functions Using GROUP BY clause
Week 5 – Chapter 5
Objectives
Group data using the GROUP BY clause
Include or exclude grouped rows results by using the HAVING clause
EMPLOYEESEMPLOYEES
maximum maximum salary in salary in
the the EMPLOYEES EMPLOYEES
tabletable
ID SALARY 100 24000 101 17000 102 17000 103 9000 …178 7000200 4400 201 13000 202 6000 205 12000 206 8300
MAXSAL
24000
Group functions without a GROUP BY clause operate on one set of rows to give one row result
Group Fns Without GROUP BY
SELECT MAX(salary) MAXSALFROM employees;
GROUP BY Clause
SELECT [column], group_function(column), ...
FROM table[WHERE condition][GROUP BY column][HAVING expression][ORDER BY column];
Divide rows into smaller subsets or groups by using the GROUP BY clause.
EMPLOYEESEMPLOYEES
maximum maximum salary in salary in
eacheach dept in dept in EMPLOYEES EMPLOYEES
tabletable
DEPT SALARY 10 4400 20 1300020 600050 5800 50 3500 50 3100 50 2500 50 2600 60 900060 600060 4200 …
7000
DEPT MAX 10 4400 20 13000 …
7000
Group functions with a GROUP BY clause operate on sets of rows to give 1 row result per set of rows
Group Fns With GROUP BY
SELECT department_id DEPT, MAX(salary) MAX FROM employeesGROUP BY department_id;
Notes on GROUP BY Clause A SELECT statement with a group function(s) and no
GROUP BY clause returns only one row as its result
A SELECT statement with a GROUP BY clause can return multiple rows as its result
If you include a group function in a SELECT clause, you cannot display values from individual rows unless the individual column is in the GROUP BY clause
The GROUP BY column does not have to be in the SELECT list; eg:
SELECT SUM(salary)
FROM employees
GROUP BY department_id
Group Data on More Than 1 ColumnData can be grouped into subsets by more than column.
For example, salary could be totaled by job_id within each department as follows:
SELECT department_id,job_id,SUM(salary)FROM employeesGROUP BY department_id, job_id
NOTE: All columns not used with group functions that are in the SELECT clause must be listed in the GROUP BY clause
DEPARTMENT_ID JOB_ID SUM(SALARY) SA_REP 7000
10 AD_ASST 4400 20 MK_MAN 1300020 MK_REP 600050 ST_MAN 5800…110 AC_ACCOUNT 8300
Group Data on more than 1 Column
If the order of the columns is changed in the GROUP BY clause then the data is simply presented in a different order. For example, salary could be totaled by department_id within each job as follows:
SELECT job_id, department_id,job_id,SUM(salary)FROM employeesGROUP BY job_id, department_id
JOB_ID DEPARTMENT_ID SUM(SALARY) AD_VP 90 34000 AC_MGR 110 12000 MK_MAN 20 13000 MK_REP 20 6000 SA_MAN 80 10500 SA_REP 7000 SA_REP 80 19600 …AC_ACCOUNT 110 8300
Group Function Error You will receive an error message if you include
a group function and GROUP BY clause and an expression referencing a non-group column in a SELECT clause. Example:
SELECT last_name, department_id, AVG(salary)
FROM employees
GROUP BY department_id;
ERROR at line 1: ORA-00979: not a GROUP BY expressionQuery does not make sense: there is only 1 value of department_id and average salary to display for many values of last name
Excluding Group Results Use the HAVING clause to exclude set(s) of results from
a GROUP BY clause (rows are grouped; group function is applied; groups matching the HAVING clause are displayed).
Example, do not display departments having 2 or less employees:
SELECT department_id DEPT, MAX(salary) MAX, COUNT(*)
FROM employeesGROUP BY department_idHAVING COUNT(*) > 2
DEPT MAX COUNT(*) 50 5800 5 60 9000 3 80 11000 3 90 24000 3
HAVING Clause Error Cannot use the HAVING clause to rows - must
reference a group function result in HAVING clause.
Example, try to exclude IT_PROG from results: SELECT department_id DEPT, MAX(salary)
MAX, COUNT(*) FROM employeesGROUP BY department_idHAVING job_id LIKE 'IT_PROG'
•ERROR at line 4: HAVING job_id LIKE 'IT_PROG' •ORA-00979: not a GROUP BY expression
HAVING Clause Notes
HAVING clause excludes result(s) for sets of rows.
Must be used with GROUP BY clause Should only reference group results (or
columns data is grouped by)
Important Notes on Group Functions
You cannot use the WHERE clause to restrict groups.
You must use the HAVING clause to restrict groups.
You cannot use group functions in the WHERE clause (WHERE clause used to limit rows, not groups of rows)
Any column or expression in the SELECT list that is not an aggregate function must be in the GROUP BY clause.
Nesting Group Functions
Group functions can be nested Example: find the highest of the average
salary for each department:SELECT MAX(AVG(salary)) MAX
FROM employeesGROUP BY department_id
MAX 19333.3333