#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 > (
select AVG(salary)
from employees
);
#3.选择工资大于所有JOB_ID = 'SA_MAN'的员工的工资的员工的last_name, job_id, salary
select last_name,job_id,salary
from employees
where salary > all (
select salary
from employees
where job_id = 'SA_MAN'
);
#4.查询和姓名中包含字母u的员工在相同部门的员工的员工号和姓名
select employee_id,last_name
from employees
where department_id = any (
select distinct department_id
from employees
where last_name like '%u%'
);
#5.查询在部门的location_id为1700的部门工作的员工的员工号
select employee_id
from employees e
where department_id in (
select department_id
from departments
where location_id = 1700
);
#6.查询管理者是King的员工姓名和工资
方法1:
select last_name,salary
from employees
where manager_id in (
select employee_id
from employees
where last_name = 'King'
);
方法2:
select last_name,salary
from employees emp
where exists (
select *
from employees mgr
where manager_id = emp.`employee_id`
and emp.last_name = 'King'
);
方法3:
select e2.last_name,e2.salary
from employees e1 join employees e2
on e1.`employee_id` = e2.`manager_id`
where e1.last_name = 'King'
#7.查询工资最低的员工信息: last_name, salary
select last_name,salary
from employees
where salary = (
select MIN(salary)
from employees
);
#8.查询平均工资最低的部门信息
#方法1
select *
from departments
where department_id = (
select department_id
from employees
group by department_id
having AVG(salary) = (
select min(sal_avg)
from (
select avg(salary) sal_avg
from employees
group by department_id) tb_sal_avg
)
);
#方法2
select *
from departments
where department_id = (
select department_id
from employees
group by department_id
having AVG(salary) = (
select avg(salary) sal_avg
from employees
group by department_id
order by sal_avg asc
limit 0,1
)
);
#方法3
select *
from departments
where department_id = (
select department_id
from employees
group by department_id
having AVG(salary) <= all (
select avg(salary) sal_avg
from employees
group by department_id
)
);
#方法4
select d.*,dept_avg_sal.avg_sal
from departments d,(
select department_id,avg(salary) avg_sal
from employees
group by department_id
order by avg_sal
limit 0,1) dept_avg_sal
where d.department_id = dept_avg_sal.department_id;
#9.查询平均工资最低的部门信息和该部门的平均工资(相关子查询)
#方法1
select d.*,(select AVG(salary) from employees where department_id = d.department_id) avg_sal
from departments d
where department_id = (
select department_id
from employees
group by department_id
having AVG(salary) = (
select MIN(dept_avg_sal)
from (
select AVG(salary) dept_avg_sal
from employees
group by department_id
)emp_avg_sal
)
);
#方法2
select d.*,(select AVG(salary) from employees where department_id = d.department_id) avg_sal
from departments d
where department_id = (
select department_id
from employees
group by department_id
having AVG(salary) = (
select AVG(salary) dept_avg_sal
from employees
group by department_id
order by dept_avg_sal
limit 0,1
)
);
#方法3
select d.*,(select AVG(salary) from employees where department_id = d.department_id) avg_sal
from departments d
where department_id = (
select department_id
from employees
group by department_id
having AVG(salary) <= all (
select AVG(salary) dept_avg_sal
from employees
group by department_id
)
);
#方法4
select d.*,emp_avg_sal.avg_sal
from departments d,(
select department_id,avg(salary) avg_sal
from employees
group by department_id
order by avg_sal
limit 0,1) emp_avg_sal
where d.department_id = emp_avg_sal.department_id
#10.查询平均工资最高的 job 信息
#方法1
select *
from jobs
where job_id = (
select job_id
from employees
group by department_id
having AVG(salary) = (
select MAX(avg_sal)
from (
select AVG(salary) avg_sal
from employees
group by department_id
)emp_avg_sal
)
);
#方法2
select *
from jobs
where job_id = (
select job_id
from employees
group by department_id
having AVG(salary) = (
select AVG(salary) avg_sal
from employees
group by department_id
order by avg_sal desc
limit 0,1
)
);
#方法3
select *
from jobs
where job_id = (
select job_id
from employees
group by department_id
having AVG(salary) >= all (
select AVG(salary) avg_sal
from employees
group by department_id
)
);
#方法4
select j.*
from jobs j,(
select job_id,avg(salary) avg_sal
from employees
group by department_id
order by avg_sal desc
limit 0,1) emp_avg_sal
where j.`job_id` = emp_avg_sal.`job_id`
#11.查询平均工资高于公司平均工资的部门有哪些?
方法1:
select *
from departments
where department_id = any (
select department_id
from employees
where department_id is not null
group by department_id
having AVG(salary) > (
select AVG(salary)
from employees
)
);
SELECT department_id
FROM employees
WHERE department_id IS NOT NULL
GROUP BY department_id
HAVING AVG(salary) > ( SELECT AVG(salary) FROM employees );
#12.查询出公司中所有 manager 的详细信息
#方法1
select employee_id,last_name,salary
from employees
where employee_id in (
select distinct manager_id
from employees
);
#方法2
select distinct mgr.employee_id,mgr.last_name,mgr.salary
from employees emp join employees mgr
on emp.`manager_id` = mgr.`employee_id`
#方法3
select employee_id,last_name,salary
from employees mgr
where exists (
select *
from employees emp
where emp.manager_id = mgr.employee_id
);
#13.各个部门中 最高工资中最低的那个部门的 最低工资是多少?
#方法1
select MIN(salary)
from employees
where department_id = (
select department_id
from employees
group by department_id
having MAX(salary) = (
select MIN(max_sal)
from (
select MAX(salary) max_sal
from employees
group by department_id
)emp_max_sal
)
);
#方法2
select MIN(salary)
from employees
where department_id = (
select department_id
from employees
group by department_id
having MAX(salary) = (
select MAX(salary) max_sal
from employees
group by department_id
order by max_sal
limit 0,1
)
);
#方法3
select MIN(salary)
from employees
where department_id = (
select department_id
from employees
group by department_id
having MAX(salary) <= all (
select MAX(salary) max_sal
from employees
group by department_id
)
);
#方法4
select MIN(salary)
from employees e,(
select department_id,MAX(salary) max_sal
from employees
group by department_id
order by max_sal
limit 0,1) dept_max_sal
where e.department_id = dept_max_sal.department_id
#14.查询平均工资最高的部门的 manager 的详细信息: last_name, department_id, email, salary
select last_name,department_id,email,salary
from employees
where department_id = (
select department_id
from employees
group by department_id
having AVG(salary) = (
select MAX(avg_sal)
from (
select AVG(salary) avg_sal
from employees
group by department_id
)dept_avg_sal
)
);
select last_name,department_id,email,salary
from employees
where employee_id in (
select distinct manager_id
from employees
where department_id = (
select department_id
from employees
group by department_id
having AVG(salary) = (
select MAX(avg_sal)
from (
select AVG(salary) avg_sal
from employees
group by department_id
)dept_avg_sal
)
)
);
select last_name,department_id,email,salary
from employees emp,(
select distinct manager_id
from employees e,(
select department_id,AVG(salary) avg_sal
from employees
group by department_id
order by avg_sal desc
limit 0,1) dept_avg_sal
where e.department_id = dept_avg_sal.department_id
)mgr_id
where emp.employee_id = mgr_id.manager_id;
select last_name,department_id,email,salary
from employees emp
where employee_id in (
select distinct manager_id
from employees e,(
select department_id,AVG(salary) avg_sal
from employees
group by department_id
order by avg_sal desc
limit 0,1) dept_avg_sal
where e.department_id = dept_avg_sal.department_id
)
#15. 查询部门的部门号,其中不包括job_id是"ST_CLERK"的部门号
#方法1
select department_id
from departments
where department_id not in (
select distinct department_id
from employees
where job_id = 'ST_CLERK'
);
#方法2
select *
from departments d
where not exists (
select distinct department_id
from employees e
where job_id = 'ST_CLERK'
and d.department_id = e.department_id
);
#16. 选择所有没有管理者的员工的last_name
select last_name
from employees emp
where manager_id not in (
select employee_id
from employees mgr
where emp.manager_id = mgr.employee_id
);
SELECT last_name
FROM employees e1
WHERE NOT EXISTS (
SELECT *
FROM employees e2
WHERE e1.manager_id = e2.employee_id );
#17.查询员工号、姓名、雇用时间、工资,其中员工的管理者为 'De Haan'
select employee_id,last_name,hire_date,salary,manager_id
from employees emp
where manager_id = (
select employee_id
from employees mgr
where last_name = 'De Haan'
and emp.manager_id = mgr.employee_id
);
select employee_id,last_name,hire_date,salary,manager_id
from employees emp
where exists (
select *
from employees mgr
where last_name = 'De Haan'
and emp.manager_id = mgr.employee_id
);
#18.查询各部门中工资比本部门平均工资高的员工的员工号, 姓名和工资(相关子查询)
#方法1
select employee_id,last_name,salary
from employees e1
where salary > (
select AVG(salary)
from employees e2
where e2.department_id = e1.`department_id`
);
#方法2
SELECT employee_id,last_name,salary
FROM employees e1, (
SELECT department_id,AVG(salary) avg_sal
FROM employees e2
GROUP BY department_id ) dept_avg_sal
WHERE e1.`department_id` = dept_avg_sal.department_id
AND e1.`salary` > dept_avg_sal.avg_sal;
#19.查询每个部门下的部门人数大于 5 的部门名称(相关子查询)
select department_name,department_id
from departments d
where 5 < (
select count(*)
from employees e
where e.department_id = d.department_id
);
select d.*
from departments d,(
select department_id,count(*) dep_count
from employees
group by department_id
having dep_count > 5) tb_dep_count
where tb_dep_count.department_id = d.`department_id`
#20.查询每个国家下的部门个数大于 2 的国家编号(相关子查询)
select country_id
from locations l
where 2 < (
select count(*)
from departments d
where l.location_id = d.location_id
);
select country_id
from locations l,(
select location_id,count(*) dep_count
from departments d
group by location_id
having dep_count > 2) tb_dep_count
where l.location_id = tb_dep_count.location_id