Chap3-多表查询
Posted zxdzxd
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Chap3-多表查询相关的知识,希望对你有一定的参考价值。
Chp3 复杂查询
key point:
数据分页
子查询
表连接
--分页查询练习
--1.查询工资最高的3名员工信息
select * from (select * from employees order by SALARY desc) where rownum <= 3;
--2.查询工资排名第5到第10的员工信息
select * from (select e.*,rownum r from employees e order by salary desc)where r between 5 and 10;
--3.查询first_name是以大写D开头的第3到第5个员工信息
select * from (select e.*,rownum r from employees e where first_name like ‘D%‘)where r between 3 and 5 ;
--子查询练习
--1.显示工资比’Allan’(first_name)高的所有员工的姓名和工资
select *from employees where salary > (select salary from employees where first_name = ‘Allan‘);
--2.显示与’Allan’(first_name)从事相同工作的员工的详细信息
select * from employees where job_id = (select job_id from employees where first_name = ‘Allan‘);
--3.查询工资大于本部门平均工资的员工基本信息
--4.显示与30号部门first_name为’Guy’员工工资相同的员工姓名和工资
select * from employees where department_id=30 and salary=(select salary from employees where first_name=‘Guy‘);
--5.查询所有工资高于平均工资(平均工资包括所有员工)的销售人员(‘SA_REP‘)
select *from employees where job_id=‘SA_REP‘ and salary > (select avg(salary) from employees) ;
--6.查询各个职位员工工资大于平均工资(平均工资包括所有员工)的人数和员工职位
select job_id,count(*) from employees where salary >(select avg(salary) from employees) group by job_id;
--7.查询各个职位员工工资大于平均工资(平均工资包括所有员工)的员工
--表连接查询练习
--1.查询在研发部(‘IT‘)工作员工的编号,姓名,工作部门,工作所在地
SELECT e.employee_id, e.first_name, d.department_name, l.street_address FROM employees e
LEFT JOIN departments d ON e.department_id = d.department_id
LEFT JOIN locations l ON d.location_id = l.location_id
WHERE d.department_name LIKE ‘IT‘;
--2.查询各个部门的名称和员工人数
SELECT e.department_id, d.department_name, COUNT(1) as 员工人数 FROM employees e
LEFT JOIN departments d ON e.department_id = d.department_id
GROUP BY e.department_id
--3.查询工资相同的员工的工资和姓名
SELECT e.employee_id, e2.employee_id, e.first_name, e2.first_name, e.salary FROM employees e
LEFT JOIN employees e2 ON e.salary = e2.salary
WHERE e.employee_id < e2.employee_id
ORDER BY e.salary
--4.显示10号部门的经理和20号部门的所有职员的详细信息
SELECT e.employee_id, e.manager_id, e.department_id FROM employees e
WHERE e.department_id = 20
OR e.employee_id = (SELECT e2.manager_id FROM employees e2 WHERE e2.department_id =10)
--5.查询员工的基本信息,附加其上级的姓名
SELECT e.employee_id, e.first_name, e.manager_id, e2.employee_id, e2.first_name FROM employees e
LEFT JOIN employees e2 ON e.manager_id = e2.employee_id
--6.求入职日期相同(年月日相同)的员工(考察知识点:自连接)
SELECT e.employee_id, e2.employee_id, e.hire_date, e2.hire_date FROM employees e
LEFT JOIN employees e2 ON e.hire_date = e2.hire_date
WHERE e.employee_id<e2.employee_id
ORDER BY e.hire_date;
--7.显示各个部门经理的基本工资
--第一步 查询各个部门的经理编号 --tb1
select * from departments d where d.manager_id is not null ;
--第二步 员工表与 tb1创建连接
select e.employee_id, d.department_id,d.manager_id, e.salary FROM employees e
INNER JOIN departments d ON e.employee_id = d.manager_id
ORDER BY d.department_id
以上是关于Chap3-多表查询的主要内容,如果未能解决你的问题,请参考以下文章