Oracle连接查询,子查询(相关子查询,嵌套子查询)
2013-04-03 13:30
344 查看
内容简介 1,Demo连接查询。2,Demo 子查询(相关子查询,嵌套子查询)。 技术与环境 操作系统: windows 语言类别: SQL之PL/SQL thankyou: sunshine, 谢谢你的默默付出 数据库: Oracle 学习软件: Oracle 10g 课程总策划: yuanbo English name: sunshine 个人主页: http://www.cnblogs.com/ylbtech/ 科研团队: ylbtech 教研团队: ylbtech 1,Demo连接查询 ? --======================================================== --ylb:Oracle --17:13 2011-12-30 --1,链接查询(传统链接、内链接) --======================================================== --总结:什么时候使用连接查询? --当需要显示的数据来源于多个表时,使用连接查询。 --一,连接查询--分传统连接与内连接 /* 1,查询员工姓名和所在部门的名称(2种 ) --传统连接 --依赖的是 ,和where */ select * from emp,dept where emp.deptno=dept.deptno select ename,dname,emp.deptno from emp ,dept where emp.deptno=dept.deptno; select * from emp a ,dept b where a.deptno=b.deptno; select a.ename,b.dname from emp a,dept b where a.deptno=b.deptno select a.ename,b.dname,a.deptno from emp a,dept b where a.deptno=b.deptno --内联接 --依赖的是 inner join on select * from emp a inner join dept b on a.deptno=b.deptno 2,查询员工姓名和所在部门的名称,要求部门编号为30(2种 ) select a.ename,b.dname from emp a inner join dept b on a.deptno=b.deptno where a.deptno=30 select a.ename,b.dname from emp a,dept b where a.deptno=b.deptno and a.deptno=30 3,查询员工姓名和部门名称,要求没有员工的部门的名称也要查询出来 --传统连接 :使用”(+)“ select * from emp a ,dept b where a.deptno(+)=b.deptno; select * from emp a ,dept b where b.deptno=a.deptno(+); --使用join连接 : 右外连接 select * from emp a right outer join dept b on a.deptno=b.deptno 左外连接 select * from emp a left outer join dept b on a.deptno=b.deptno; 全外连接: --只能用标准sql来书写 select * from emp a full outer join dept b on a.deptno=b.deptno; 4,查询员工姓名和其直接上级的姓名,要求没有经理的员工也查询出来 --自连接 员工 SMITH 的上级是 FORD select '员工'||a.ename||'的上级是'||b.ename from emp a left outer join emp b on a.mgr=b.empno; select '员工'||a.ename||'的上级是'||b.ename from emp a,emp b where a.mgr=b.empno(+) select a.ename,b.ename from emp a,emp b where a.mgr=b.empno(+) 2,Demo 子查询(相关子查询,嵌套子查询) ? --======================================================== --ylb:Oracle --17:13 2011-12-30 --1,子查询(嵌套子查询、相关子查询) --======================================================== /*** 连接与子查询的区别: 1,当需要多个表的数据时用连接,子查询只能返回单表数据。 2,连接快,子查询慢。 3,子查询功能强大。 4,子查询-两种(嵌套子查询,关联子查询) 嵌套简单,关联复杂,面试关联查询 **/ --一, 子查询第一种 : 嵌套子查询:简单--子查询可以独立运行,自内而外 --1,查询工资高于SMITH工资的所有员工 select * from emp where sal>(select sal from emp where enAme='SMITH') go --2,查询工资高于公司平均工资的所有员工? select * from emp where sal>(select avg(sal) from emp) --附加题, --> >= < <= = != <> ^= 后面只能跟一个值, --如果有多个值,>all--大于最大值 >any--大于最小值 --查询工资高于所有部门的平均工资的员工 select * from emp where sal>all(select avg(sal) from emp group by deptno) --查询工资高于任何部门的平均工资的员工 select * from emp where sal>any(select avg(sal) from emp group by deptno) go /*********************************************************************************/ --二, 子查询第二种 : 关联子查询 ,思考:自外而内 --3,查询工资高于本部门平均工资的所有员工? select * from emp a where a.sal>(select avg(sal) from emp where deptno=a.deptno) --4,查询本部门最高工资的员工?(三种方法) --方法一,使用嵌套子查询(非关联子查询) select * from emp a where (a.deptno,a.sal) in (select deptno,max(sal) from emp group by deptno) --方法二,使用关联子查询/*9-****************** select * from emp a where a.sal=(select max(sal) from emp where deptno=a.deptno) --方法三,使用关联子查询的名次问题,名次=人数+1 sal=800 deptno=20 select * from emp a where ( select count(*) from emp where deptno=a.deptno and sal>a.sal)=1 /*********************************************************************************/ go --补充题: --查询本部门第二高工资的员工?(一种方法) --5,查询本部门最低工资的员工 ? select * from emp a where (select count(*) from emp where deptno=a.deptno and sal<a.sal)=0 ------------------------------------------------------三,select 语句做表达式 --6,统计每个部门的信息和人数? select a.*,(select count(*) from emp where deptno=a.deptno) 人数 from dept a select a.* from dept a select a.deptno,b.dname,b.loc,count(*) from emp a,dept b where a.deptno=b.deptno group by a.deptno,b.dname,b.loc --7,统计每个部门工资在(500-1000)/(1000-3500)/(3500-7000) 的人数? select a.*, (select count(*) from emp where deptno=a.deptno and sal>500 and sal<=1000) "500-1000", (select count(*) from emp where deptno=a.deptno and sal>1000 and sal<=3500), (select count(*) from emp where deptno=a.deptno and sal>3500 and sal<=7000) from dept aregexp_replace(1,2,3,4,5,6)
语法说明:1:字段 2:替换的字段 3:替换成什么 4:起始位置(默认从1开始) 5:替换的次数(0是无限次) 6:不区分大小写
原文:http://www.cnblogs.com/ylbtech/archive/2012/08/09/2630705.html
相关文章推荐
- Oracle连接查询,子查询(相关子查询,嵌套子查询)
- Oracle: 四、Oracle连接查询,子查询(相关子查询,嵌套子查询)
- oracle sql 多表 嵌套子查询 连接查询, join where exist in 的区别
- Oracle: 四、Oracle连接查询,子查询(相关子查询,嵌套子查询)(下)
- oracle 查询操作用的相关信息 当前操作人 操作人主机名称 主机ip 连接oracle 外网ip
- Oracle 相关子查询
- Oracle多表连接查询区别
- Apache PHP Smarty PDO连接oracle 相关配置
- oracle心得4--集合查询@oracle中的多表连接@案例分析
- oracle左连接,右连接查询 (+)
- oracle order by subquery innerview(嵌套子查询中不允许出现order by 语句)
- 数据库(学习整理)----7--Oracle多表查询,三种join连接
- ORACLE 多表连接与子查询
- Oracle学习(七)----查询Top—N问题 & 相关子查询
- 嵌套子查询总存在一个等价的连接查询?
- Oracle学习_3 sql子查询,自连接,单行select总结
- 数据库Oracle之多表查询:笛卡尔积、等值连接、不等值连接、外连接,自连接
- oracle 连接相关
- oracle连接数问题相关,session状态说明
- oracle多表查询之内连接,外连接语句总结