Advanced # 5: grouping query / * the SELECT group functions, column (required appear after the group by) from table [where] the filter criteria list group by grouping the order by clause] [ Note: The list of queries have special requirements and after the field grouping function group by the features: the filter criteria in the query packet 1 into two data source locations keyword filtering group by the original table where the front of the packet having the grouping result after the filter set group by the grouping packets function does conditions, must be placed in the having clause group by clause supports a single field of the packet, a plurality of packet fields (a plurality of fields separated by commas no sequence requirement), the expression or function (with less ) You can also add sorting (sort on a query packet last) * / # introduced: the average salary for each department query select avg (salary) from employees; # this is the entire table of basic salary # need to split the group select avg ( salary), department_id from employees group by department_id; # this result is unlikely to select distinct department_id from employees; Case # 1: Query highest salary for each type of work the SELECT max (salary), job_id from the Employees Group by job_id; # Case 2: the number of queries department at each position select count (department_id), location_id from departments group by location_id; # Add the packet filtering criteria before # case 1: check the mailbox contains a character, the average salary for each department of the SELECT AVG (salary), the WHERE department_id from the Employees Email like "% a%" Group by department_id; # case 2: query there are bonuses for each employee leading men of the highest wages the SELECT max (salary), manager_id from the employees commission_pct the WHERE iS not null Group by manager_id; # add complex filter criteria grouped # case 1: query the number of employees of the department> 2 the SELECT COUNT (*), department_id from employees the wHERE count (*)> 2 Group by department_id; # this is wrong, because employees did not count (*)> 2 used here having COUNT the SELECT (*), department_id from the Employees Group by department_id the HAVING COUNT (*)> 2; # Case 2: query each type of work employees bonuses, their highest salary> 12,000 number of jobs and the highest salaries select max (salary) , job_id from employees where commission_pct is not null group by job_id; # get there is the bonus conditions under each category of work of the highest wages # plus: their highest salary> 12,000 number of jobs and the highest salaries select max (salary), the Employees from the WHERE iS commission_pct job_id not null Group by job_id the HAVING max (salary)> 12000; # case 3: query leading number> minimum wage every leader of 102 men> 5000 which led number, and the minimum wage screening conditions: WHERE the manager_id> 102 HAVING min (the salary)> 5000 full: SELECT min (the salary), the manager_id the manager_id from the Employees WHERE> Group by the manager_id HAVING 102 min (the salary)> 5000; # Group followed by expression or function group (which may be is not a simple field) # case: according to the length of the packet employee name, query the number of employees for each group, sieve The number of employees> COUNT the SELECT (employee_id), length (last_name) from the Employees Group by length (last_name) the HAVING COUNT (employee_id)> 5; # packets in accordance with a plurality of fields Case #: Query the average wage of employees of each department for each type of work the SELECT AVG (salary), department_id, job_id from the Employees Group by department_id, job_id; # add sorting # Case: Query the average wage of employees in each department in each trade and shows the level of average wages in accordance with the SELECT AVG (salary), department_id, job_id from the Employees Group by department_id, job_id the Order by AVG (salary) desc; the Order by the statement with the back can function. Title # 1: for each query job_id maximum wages, minimum, average, sum, and in accordance with ascending job_id select max (salary), min ( salary), avg (salary), sum (salary), job_id from employees by the Order by job_id job_id Group asc; # topic 2: query highest wage gap between employees and the minimum wage (-difference) the SELECT max (salary) -min (salary) -difference from the employees; WHERE IS Not the manager_id null HAVING min (the salary)> = 6000 Topic # 3: Query the minimum wage each manager his employee, where the minimum wage can not be less than 6000, no management staff are not counted. Filters: the SELECT min (salary), manager_id from the Employees manager_id the WHERE IS not null Group by manager_id the HAVING min (salary)> = 6000; # 4 Topic: Query the average number of all sectors, the number of employees and wages, and according to the average wages descending the sELECT COUNT (employee_id), AVG (salary), department_id from the employees Group by department_id the Order by AVG (salary) desc; # topic 5: select the number of employees has each job_id of select count (*), job_id from employees group by job_id ;