MySQL syntax ------ 12 ----- subquery (b)

Second, on the back of the sub-select query query only supports scalar 
Case 1: Query the number of employees in each department 
select d *, (select count ( 1) from employees e where e.department_id = d.department_id) from departments. d; - 27 rows is not displayed some departments of employees 0 
SELECT d.department_id, COUNT (. 1) from the employees E, D WHERE e.department_id = d.department_id departments by d.department_id Group - all rows. 11 all employees of department 
case 2: query = number of employees in the department name 102 
select (select d.department_name from employees e INNER join departments d on e.department_id = d.department_id where e.employee_id = '102') department name; 

Third, on the back from the select statement 
query results serve as a table, you must surnamed 
case 1: query the average salary for each department of the wage scale 
step one: Search the average salary for each department 
select avg (salary), department_id from employees GROUP BY department_id; 
step two: wage level 
select * from job_grades; 
step three: integrally bonded
select ag_dep.*,g.grade_level 
from (select avg(salary) ag,department_id from employees GROUP BY department_id) ag_dep 
INNER JOIN job_grades g 
on ag_dep.ag BETWEEN lowest_sal and highest_sal;

  

Guess you like

Origin www.cnblogs.com/dongyaotou/p/12339765.html