ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

mysql 子查询练习题

2022-07-13 21:31:36  阅读:169  来源: 互联网

标签:练习题 salary employees 查询 mysql department avg id select


#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

标签:练习题,salary,employees,查询,mysql,department,avg,id,select
来源: https://www.cnblogs.com/xiangbing123/p/16475662.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有