码迷,mamicode.com
首页 > 数据库 > 详细

oracle多表查询

时间:2018-11-04 21:53:22      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:sql   sel   count   name   工资   结果   dal   没有   笛卡尔   

oracle多表查询

--1.DEPT表按照部门跟EMP表左关联
select ename,sal,dname,e.deptno from emp e left outer join dept d on e.deptno = d.deptno
select ename,sal,dname,e.deptno from emp e , dept d where e.deptno = d.deptno(+)

--2. 列出所有“CLERK”(办事员)的姓名及其部门名称。 
--笛卡尔积
select ename,dname,job from emp e,dept d where e.deptno = d.deptno and job = 'CLERK'
--内连接
select ename,dname,job from emp e inner join dept d on e.deptno = d.deptno and job = 'CLERK'
select ename,dname,job from emp e join dept d on e.deptno = d.deptno and job = 'CLERK'
select ename,dname,job from emp e join dept d using(deptno) where job = 'CLERK'
--自然连接
select ename,dname,job from emp e natural join dept d where job = 'CLERK'
--外连接(不合适)
--select ename,dname,job from emp e right outer join dept d on e.deptno = d.deptno and job = 'CLERK'

--3. 列出所有雇员的雇员名称、部门名称和薪金(使用自然连接,using字句,on字句)
--自然连接
select ename,dname,sal from emp e natural join dept d
--using子句(inner可省略)
--内连接
select ename,dname,sal from emp e inner join dept d using(deptno)
select ename,dname,sal from emp e join dept d using(deptno)
--外连接
select ename,dname,sal from emp e left outer join dept d using(deptno)
select ename,dname,sal from emp e right outer join dept d using(deptno) 
--on子句(inner可省略)
--内连接
select ename,dname,sal from emp e  inner join dept d on e.deptno = d.deptno
select ename,dname,sal from emp e  join dept d on e.deptno = d.deptno 
--外连接
select ename,dname,sal from emp e left outer join dept d on e.deptno = d.deptno
select ename,dname,sal from emp e right outer join dept d on e.deptno = d.deptno
select ename ,dname,sal from emp e,dept d where e.deptno = d.deptno(+)
select ename ,dname,sal from emp e,dept d where e.deptno(+) = d.deptno
select ename,dname,sal from emp e full outer join dept d on e.deptno = d.deptno
    
--4. 列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门
--右外连接
select dname,e.* from emp e right outer join dept d on e.deptno = d.deptno

--5. 列出所有员工的姓名及其直接上级领导的姓名 
--笛卡尔积
select e.ename 员工姓名,b.ename 老板姓名 from emp e, emp b where e.mgr = b.empno
--内连接
select e.ename 员工姓名, b.ename 老板姓名 from emp e inner join emp b on e.mgr = b.empno
--外连接
--左外连接
select e.ename 员工姓名, b.ename 老板姓名 from emp e left outer join emp b on e.mgr = b.empno
select e.ename 员工姓名, b.ename 老板姓名 from emp e , emp b where e.mgr = b.empno(+)
--右外连接(不合适)
--select e.ename 员工姓名, b.ename 老板姓名 from emp e right outer join emp b on e.mgr = b.empno
--select e.ename 员工姓名, b.ename 老板姓名 from emp e , emp b where e.mgr(+) = b.empno

--6. 显示所有员工的姓名ename,部门号deptno和部门名称dname。(使用自然连接,using字句,on字句)
--自然连接
select ename,deptno,dname from emp e natural join dept d
--using
select ename ,deptno , dname from emp e join dept d using(deptno)
select ename,deptno , dname from emp e left outer join dept d using(deptno)
--on
select ename,d.deptno,dname from emp e join dept d on e.deptno = d.deptno
select ename ,d.deptno,dname from emp e left outer join dept d on e.deptno = d.deptno

--7. 查询20号部门员工的job和20号部门的loc 20;
select job,loc from emp e join dept d on e.deptno = d.deptno and d.deptno = 20

--8. 选择所有有奖金comm的员工的ename , dname , loc  
select ename,dname,loc from emp e join dept d on e.deptno = d.deptno and comm is not null; 

--9.选择在DALLAS工作的员工的ename , job , deptno, dname   
select ename,job ,d.deptno, dname from emp e join dept d on e.deptno = d.deptno and loc = 'DALLAS'

--10. 显示雇员名、雇员工资及所在部门的名字,并按部门排序。(使用自然连接,using字句,on字句)
--自然连接
select ename,sal,dname from emp e natural join dept d order by job
--using
select ename ,sal , dname from emp e join dept d using(deptno) order by job
select ename,sal , dname from emp e left outer join dept d using(deptno) order by job
--on
select ename,sal,dname from emp e join dept d on e.deptno = d.deptno order by job
select ename ,sal,dname from emp e left outer join dept d on e.deptno = d.deptno order by job

--11. 列出至少有一个员工的所有部门的部门名称(DNAME)和该部门的员工人数(使用自然连接,using字句,on字句)
--自然连接
select dname,count(empno) 员工人数 from emp e natural join dept d group by dname 
--on子句
select dname,count(empno) 员工人数 from emp e left outer join dept d on e.deptno = d.deptno group by dname
--using子句
select dname,count(empno) 员工人数 from emp e left outer join dept d using(deptno) group by dname

--12. 查询受雇日期早于其直接上级的所有员工的编号,姓名,部门名称
select empno,h.hname,dname from dept d , 
(select e.empno,e.ename as hname,b.ename,e.deptno as dep from emp e join emp b on e.mgr = b.empno and 
e.hiredate < b.hiredate) h
where d.deptno = h.dep

--13. 查询至少有4个员工的部门的部门名称。(使用自然连接,using字句,on字句)
--自然连接
select dname from emp e natural join dept d group by dname having count(empno) > 4
--using子句
select dname from emp e join dept d using(deptno) group by dname having count(empno) > 4
--on子句
select dname from emp e join dept d on e.deptno = d.deptno group by dname having count(empno) > 4

--14. 查询部门名称中带'S'字符的部门的员工的工资总和部门人数,显示结果为部门名称,部门员工的工资总和,部门人数
select dname,sum(sal),count(empno) from emp e join dept d on e.deptno = d.deptno and dname like '%S%' group by dname

--15. 查询出没有下属的员工的姓名及他的职位
 select b.ename,b.job from emp b left outer join emp e on e.mgr = b.empno where e.ename is null

oracle多表查询

标签:sql   sel   count   name   工资   结果   dal   没有   笛卡尔   

原文地址:https://www.cnblogs.com/hglibin/p/9905983.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!