Presentation is loading. Please wait.

Presentation is loading. Please wait.

After completing this lesson, you should be able to do the following: Identify the available group functions Describe the use of group functions Group.

Similar presentations


Presentation on theme: "After completing this lesson, you should be able to do the following: Identify the available group functions Describe the use of group functions Group."— Presentation transcript:

1

2 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 using the GROUP BY clause Include or exclude grouped rows by using the HAVING clause

3 Group functions operate on sets of rows to give one result per group. Employee “maximum salary in the Employee table” dept_nbr salary --------- 10 2450 10 5000 10 1300 20 800 20 1100 20 3000 20 2975 30 1600 30 2850 30 1250 30 950 30 1500 30 1250 MAX(salary) ----------- 5000

4 AVG COUNT MAX MIN STDDEV SUM VARIANCE

5 SELECT[column,] group_function(column) FROMtable [WHEREcondition] [GROUP BYcolumn] [ORDER BYcolumn];

6 You can use AVG and SUM for numeric data. AVG(salary) MAX(salary) MIN(salary) SUM(salary) ----------- ---------- ----------- ----------- 1400 1600 1250 5600 MySQL>SELECTAVG(salary), MAX(salary), ->MIN(salary), SUM(salary) ->FROMemployee ->WHEREjob LIKE 'salaryes';

7 You can use MIN and MAX for any datatype. MySQL>SELECTMIN(hire_date), MAX(hire_date) ->FROMemployee; MIN(HIRED) MAX(HIRED) --------- 17-DEC-80 12-JAN-83

8 COUNT(*) returns the number of rows in a table. COUNT(*) --------- 6 MySQL>SELECTCOUNT(*) ->FROMemployee ->WHEREdept_nbr = 30;

9 COUNT(expr) returns the number of nonnull rows. MySQL>SELECTCOUNT(commission) ->FROMemployee ->WHEREdept_nbr = 30; COUNT(Commission) ----------------- 4

10 Group functions ignore null values in the column. MySQL>SELECT AVG(commission) ->FROM employee; AVG(Commission) ---------------- 550

11 The NVL function forces group functions to include null values. MySQL>SELECT AVG(IFNULL(commission,0)) ->FROM employee; Average_Commission ------------------ 157.14286

12 Employee “average salary in Employee table for each department” 2916.6667 2916.6667 2175 2175 1566.6667 1566.6667 dept_nbr salary --------- -------- 10 2450 10 5000 10 1300 20 800 20 1100 20 3000 20 2975 30 1600 30 2850 30 1250 30 950 30 1500 30 1250 dept_nbr AVG(salary) -------- ----------- 10 2916.6667 20 2175 30 1566.6667

13 Divide rows in a table into smaller groups by using the GROUP BY clause. SELECTcolumn, group_function(column) FROMtable [WHEREcondition] [GROUP BYgroup_by_expression] [ORDER BYcolumn];

14 All columns in the SELECT list that are not in group functions must be in the GROUP BY clause. MySQL>SELECT dept_nbr, AVG(salary) ->FROM employee ->GROUP BY dept_nbr; dept_nbr AVG(salary) --------- 10 2916.6667 20 2175 30 1566.6667

15 The GROUP BY column does not have to be in the SELECT list. MySQL>SELECT AVG(salary) ->FROM employee ->GROUP BY dept_nbr ; AVG(salary) --------- 2916.6667 2175 1566.6667

16 Employee “sum salaries in the Employee table for each job, grouped by department” dept_nbr Job salary --------- --------- --------- 10 Manager 2450 10 President 5000 10 Clerk 1300 20 Clerk 800 20 Clerk 1100 20 Analyst 3000 20 Manager 2975 30 Salesman 1600 30 Manager 2850 30 Salesman 1250 30 Clerk 950 30 Salesman 1500 30 Salesman 1250 Job SUM(salary) --------- Clerk 1300 Manager 2450 President 5000 Analyst 6000 Clerk 1900 Manager 2975 Clerk 950 Manager 2850 Salesman 5600 dept_nbr -------- 10 20 30

17 MySQL> SELECT dept_nbr, job, sum(salary) -> FROM employee -> GROUP BY dept_nbr, job; dept_nbr job SUM(salary) --------- --------- ----------- 10 Clerk 1300 10 Manager 2450 10 President 5000 20 Analyst 6000 20 Clerk 1900... 9 rows selected.

18 Any column or expression in the SELECT list that is not an aggregate function must be in the GROUP BY clause. MySQL> SELECT dept_nbr, COUNT(name) -> FROM employee; MySQL> SELECT dept_nbr, COUNT(name) -> FROM employee; dept_nbrcount(name) ------------------- 20 14 dept_nbrcount(name) ------------------- 20 14 Column missing in the GROUP BY clause

19 You cannot use the WHERE clause to restrict groups. You use the HAVING clause to restrict groups. MySQL> SELECT dept_nbr, AVG(salary) -> FROM employee -> WHERE AVG(salary) > 2000 -> GROUP BY dept_nbr; MySQL> SELECT dept_nbr, AVG(salary) -> FROM employee -> WHERE AVG(salary) > 2000 -> GROUP BY dept_nbr; ERROR 111: Invalid use of group functions Cannot use the WHERE clause to restrict groups to restrict groups Cannot use the WHERE clause to restrict groups to restrict groups

20 “maximum salary per department greater than $2900” EMP 5000 3000 2850 dept_nbr salary --------- 10 2450 10 5000 10 1300 20 800 20 1100 20 3000 20 2975 30 1600 30 2850 30 1250 30 950 30 1500 30 1250 dept_nbr MAX(salary) --------- ---------- 10 5000 20 3000

21 Use the HAVING clause to restrict groups Rows are grouped. The group function is applied. Groups matching the HAVING clause are displayed. SELECTcolumn, group_function FROMtable [WHEREcondition] [GROUP BYgroup_by_expression] [HAVINGgroup_condition] [ORDER BYcolumn];

22 MySQL> SELECT dept_nbr, max(salary) -> FROM employee -> GROUP BY dept_nbr -> HAVING max(salary)>2900; dept_nbr MAX(salary) --------- ----------- 10 5000 20 3000

23 MySQL> SELECT job, SUM(salary) PAYROLL -> FROM employee -> WHERE job NOT LIKE 'salaryies' -> GROUP BY job -> HAVING SUM(salary)>5000 -> ORDER BY SUM(salary); JOB PAYROLL --------- Analyst 6000 Manager 8275 5 HAVING SUM(salary)>5000

24 Display the maximum average salary. MySQL> SELECT max(avg(salary)) -> FROM employee -> GROUP BY dept_nbr; MAX(AVG(salary)) ------------- 2916.6667

25 Order of evaluation of the clauses: WHERE clause GROUP BY clause HAVING clause SELECTcolumn, group_function(column) FROMtable [WHEREcondition] [GROUP BYgroup_by_expression] [HAVINGgroup_condition] [ORDER BYcolumn];


Download ppt "After completing this lesson, you should be able to do the following: Identify the available group functions Describe the use of group functions Group."

Similar presentations


Ads by Google