#1.查询和Zlotkey相同部门的员工姓名和工资SELECT last_name,salary
FROM employees
WHERE department_id =(SELECT department_id
FROM employees
WHERE last_name ='Zlotkey');#2.查询工资比公司平均工资高的员工的员工号,姓名和工资。SELECT employee_id,last_name,salary
FROM employees
WHERE salary >(SELECTAVG(salary)FROM employees
);#3.选择工资大于所有JOB_ID = 'SA_MAN'的员工的工资的员工的last_name, job_id, salarySELECT last_name,job_id,salary
FROM employees
WHERE salary >ALL(SELECT salary
FROM employees
WHERE job_id ='SA_MAN');#4*.查询 名字包含'%u%' 的 部门, 打印与其同部门的员工id,名字SELECT employee_id,last_name
FROM employees
WHERE department_id =ANY(SELECTDISTINCT department_id
FROM employees
WHERE last_name LIKE'%u%');#5*.查询在部门的location_id为1700的部门工作的员工的员工号SELECT employee_id
FROM employees
WHERE department_id IN(SELECT department_id
FROM departments
WHERE location_id =1700);#6.查询管理者是King的员工姓名和工资SELECT e.last_name,e.salary,m.last_name
FROM employees e, employees m
WHERE e.manager_id = m.employee_id
AND m.last_name ='king';SELECT last_name,salary,last_name
FROM employees
WHERE last_name IN(SELECT last_name
FROM employees
WHERE last_name ='king');#7*.查询工资最低的员工信息: last_name, salarySELECT last_name,salary
FROM employees
WHERE salary <=ALL(SELECT salary
FROM employees
);SELECT last_name,salary
FROM employees
WHERE salary =(SELECTMIN(salary)FROM employees
);#8*.查询平均工资最低的部门信息# 方式一:SELECT*FROM departments d1 ,(SELECTAVG(salary) avg_sal,department_id
FROM employees
GROUPBY department_id
ORDERBY avg_sal ASCLIMIT0,1) d2
WHERE d1.department_id = d2.department_id;# 方式二# 方式三# 方式四#9.查询平均工资最低的部门信息和该部门的平均工资(相关子查询)SELECT d1.*,d2.department_id,d2.avg_sal
FROM departments d1 ,(SELECTAVG(salary) avg_sal,department_id
FROM employees
GROUPBY department_id
ORDERBY avg_sal ASCLIMIT0,1) d2
WHERE d1.department_id = d2.department_id;#10.查询平均工资最高的 job 信息SELECT j.*,e.avg_sal
FROM jobs j,(SELECTAVG(salary) avg_sal,job_id
FROM employees
GROUPBY job_id
ORDERBY avg_sal DESCLIMIT0,1) e
WHERE j.job_id = e.job_id;#11.查询平均工资高于公司平均工资的部门有哪些?SELECT department_id
FROM employees
WHERE department_id ISNOTNULLGROUPBY department_id
HAVINGAVG(salary)>(SELECTAVG(salary)FROM employees
);#12.查询出公司中所有 manager 的详细信息SELECTDISTINCT m.*FROM employees e,employees m
WHERE e.manager_id = m.employee_id
#13.各个部门中 最高工资中最低的那个部门的 最低工资是多少?SELECT e1.department_id,e2.max_sal
FROM employees e1 ,(SELECTMAX(salary) max_sal,department_id
FROM employees e2
GROUPBY department_id
ORDERBY max_sal
LIMIT0,1)e2
WHERE e1.department_id = e2.department_id;#14.查询平均工资最高的部门的 manager 的详细信息: last_name, department_id, email, salarySELECT employee_id, last_name,m.department_id,email,salary
FROM employees e ,(SELECTAVG(salary) avg_sal,department_id
FROM employees m
GROUPBY department_id,employee_id
ORDERBY avg_sal DESCLIMIT0,1) m
WHERE e.department_id = m.department_id
LIMIT0,1;#15. 查询部门的部门号,其中不包括job_id是"ST_CLERK"的部门号SELECT department_id
FROM departments
WHERE department_id NOTIN(SELECTDISTINCT department_id
FROM employees
WHERE job_id ='ST_CLERK');#16. 选择所有没有管理者的员工的last_nameSELECT last_name
FROM employees e1
WHERENOTEXISTS(SELECT*FROM employees e2
WHERE e1.manager_id = e2.employee_id
);#17.查询员工号、姓名、雇用时间、工资,其中员工的管理者为 'De Haan'SELECT e.employee_id,e.last_name,e.hire_date,e.salary
FROM employees e JOIN employees m
WHERE e.manager_id = m.employee_id
AND m.last_name ='De Haan';#18.查询各部门中工资比本部门平均工资高的员工的员工号, 姓名和工资(相关子查询)SELECT employee_id,last_name,salary
FROM employees e1
WHERE salary >(SELECTAVG(salary)FROM employees e2
WHERE e1.department_id = e2.department_id
);#19.查询每个部门下的部门人数大于 5 的部门名称(相关子查询)SELECT department_name,department_id
FROM departments d
WHERE5<(SELECTCOUNT(*)FROM employees e
WHERE d.department_id = e.department_id
);#20.查询每个国家下的部门个数大于 2 的国家编号(相关子查询) SELECT country_id
FROM locations l
WHERE2<(SELECTCOUNT(*)FROM departments d
WHERE l.location_id = d.location_id
);