3.子查询(难):

当进行查询的时候,发现需要的数据信息不明确,需要先通过另一个查询得到,

此查询称为子查询;

执行顺序:先执行子查询得到结果以后返回给主查询

组成部分:

1).主查询部分

2).子查询部分

【注意事项】:

子查询一定需要被定义/包裹在小括号内部,可以认为是显示的提升了代码执行的优先级

需求1:

查询薪资比Abel的高的有谁?

分析:

①.先查询出Abel的薪资是多少?

②.将过滤条件定义为>①,然后进行查询得到最终需要的结果

代码实现:

select last_name,salary

from employees

where salary > (

select salary from employees

where last_name = 'Abel'

);

需求2:

查询job_id与141号员工相同,salary比143号员工多的员工的姓名,job_id和salary?

代码实现:

select last_name,job_id,salary

from employees

where job_id = (

select job_id

from employees

where employee_id = 141

)

and salary > (

select salary

from employees

where employee_id = 143

);

课堂练习:

1).返回公司工资最少的员工的employee_id,job_id和salary

select employee_id,job_id,salary

from employees

where salary = (

select min(salary)

from employees

);

2).查询平均工资高于公司平均工资的部门有哪些

select department_id,avg(salary)

from employees

group by department_id

having avg(salary) > (

select avg(salary)

from employees

)

order by department_id desc;

3).查询最低工资大于20号部门最低工资的部门id和最低工资

select department_id,min(salary)

from employees

where department_id is not null

group by department_id

having min(salary) > (

select min(salary)

from employees

having department_id = 20

);

4).返回其它职位中比job_id为'IT_PROG'中最低工资低的员工的员工号,姓名,job_id以及salary

select employee_id,last_name,job_id,salary

from employees

where salary < (

select min(salary)

from employees

where job_id = 'IT_PROG'

);

4.多表查询/多表联查

概念:

使用场景,如果一条select语句中需要查询的列遍布多张数据表,

那么我们就必须使用多表查询了!!

分类:

等值连接和非等值连接

对于等值连接分方向:

1).内连接:返回多张表中共同满足的数据,取交集

2).外连接(左、右、满):返回内连接数据的同时还会继续返回某张表中不匹配的一些记录数

3).自连接:从始至终都是一张表,模拟一张表派生为两张(它们的结构式一模一样的),自己连自己

等值连接中的内连接:

需求:

查询所有员工的员工号、员工姓名以及部门的名字?

select employee_id,last_name,department_name

from employees,departments;

【注意】

以上查询得到了2889条记录,很多都是没有用的数据(脏数据),

出现的原因是:没有添加有效的连接条件导致的,

而这种现象我们称为笛卡尔集现象;

我们日后的学习和开发环境中是绝对要避免的!!

如何保证我们之后的多表查询绝对不会出现笛卡尔集现象?

1).不能不写连接条件

2).连接条件必须是有效的

思考:如何修改上述的代码?

代码实现如下:

select employee_id,last_name,department_name

from employees,departments

where employees.department_id = departments.department_id;

需求:使用内连接来实现

查询员工的员工号、姓名、部门号、部门名字?

select employee_id,last_name,department_id,department_name

from employees,departments

where employees.department_id = departments.department_id;

以上代码出错了,出错原因:

因为对于department_id这个列在employees和departments两张表中都存在,

所以需要显示的告诉编译器,我从哪张表中获取数据内容的!

修改代码如下:

select employee_id,last_name,departments.department_id,department_name

from employees,departments

where employees.department_id = departments.department_id;

select employee_id,last_name,employees.department_id,department_name

from employees,departments

where employees.department_id = departments.department_id;

思考:没有重复的列可以使用名字.的形式来定义吗?---> 可以的

select employee.employee_id,employee.last_name,employees.department_id,departments.department_name

from employees,departments

where employees.department_id = departments.department_id;

上述代码运行以及结果方面不存在问题,但是在代码量上比较冗余!!我们可以使用如下的方式解决...

给名字起别名的方式:

修改代码如下:

select e.employee_id,e.last_name,e.department_id,d.department_name

from employees e,departments d

where e.department_id = d.department_id;

总结:对于多表查询,如果涉及n张表,至少需要有n-1个连接条件;

非等值连接:

需求:

查询员工的姓名、薪资以及薪资的等级

select last_name,salary,grade_level

from employees,job_grades

where salary between lowest_sal and highest_sal;

以上代码有问题,可以看到各个人的薪资等级,但是由于没有追加连接连接,还是出现了笛卡尔集现象;

我们需要慎用!一般之后非等值连接用的比较少,而且必须配合等值连接一起用;

学习python第四天——Oracle查询的更多相关文章

  1. 学习python第四天——Oracle分组

    1.分组的概念: 关键字:group by子句 结论:在select列表中如果出现了聚合函数,不是聚合函数的列,必须都要定义到group by子句的后面 需求: 查询公司各个部门的平均工资? sele ...

  2. Solr学习总结(四)Solr查询参数

    今天还是不会涉及到.Net和数据库操作,主要还是总结Solr 的查询参数,还是那句话,只有先明白了solr的基础内容和查询语法,后续学习solr 的C#和数据库操作,都是水到渠成的事.这里先列出sol ...

  3. oracle学习笔记(四)oracle内存优化

    emca -config dbcontrol db -repos recreate 解决'oracle Environment variable ORACLE_SID not defined. Ple ...

  4. 【Python】Java程序员学习Python(四)— 内置方法和内置变量

    <假如爱有天意> 当天边那颗星出现,你可知我又开始想念,有多少爱恋只能遥遥相望,就像月光洒向海面,年少的我们曾以为,相爱的人就能到永远,当我们相信情到深处在一起,听不见风中的叹息,谁知道爱 ...

  5. 学习python,第四篇:Python 3中bytes/string的区别

    原文:http://eli.thegreenplace.net/2012/01/30/the-bytesstr-dichotomy-in-python-3 python 3中最重要的新特性可能就是将文 ...

  6. 系统学习python第四天学习笔记

    1.解释 / 编译补充 编译型:代码写完后,编译器将其变成成另外一个文件,然后交给计算机执行. 解释型:写完代码交给解释器,解释器会从上到下一行行代码执行:边解释边执行. [实时翻译] 2.字符串功能 ...

  7. 学习Python第四天

    关于剩下的数据类型:字符串 字符串是有序的,不可变的(不可变的意思是指将变量a重新赋值后不会覆盖原来的值,而是在内存中开辟了一块新的内存地址,存储变量的值) 字符串的各种方法: 1,将字符串中的大写变 ...

  8. 学习python第四天 列表

    模块的导入是使用 import sys#导入模块sysprint(sys.path)#打印环境变量,可能存在的目录print(sys.argv)#打印脚本的名字,相对路径 import os os.s ...

  9. 【Python】Java程序员学习Python(五)— 函数的定义和使用

    不想做一个待宰的羔羊!!!!要自己变得强大.... 函数的定义和使用放在最前边还是有原因的,现在语言趋于通用,基本类型基本都是那些,重点还是学习对象的使用方法,而最根本的还是方法的使用,因此优先介绍, ...

随机推荐

  1. 【转】Spring事务异常回滚,捕获异常不抛出就不会回滚

    最近遇到了事务不回滚的情况,我还考虑说JPA的事务有bug? 我想多了.......     为了打印清楚日志,很多方法我都加tyr catch,在catch中打印日志.但是这边情况来了,当这个方法异 ...

  2. Golang beego ORM + CRUP 操作详解

      构建beego Web 项目         首先构建一个beego 的web 项目,这个我们完全可以参考beego 官网中的开发文档,上面介绍的非常的详细,在这我就不给大家介绍,主要是介绍ORM ...

  3. Java Web整合开发王者归来(JSP + Servlet + Struts + Hibernate + Spring) - 读书笔记

    第1章 状态码表示响应类型: 保留 表示请求成功地接收 完成请求客户需进一步细化请求 客户错误 服务器错误 Web服务器: Apache服务器,特长是处理静态页面,效率非常高. Tomcat提供对JS ...

  4. spring代理例子

    ---------------------------------------------------------- 先来看一下目录结构 显然service里面有两个java文件,UserDao是接口 ...

  5. LeetCode 545----Boundary of Binary Tree

    Given a binary tree, return the values of its boundary in anti-clockwise direction starting from roo ...

  6. 图片加载库Glide的封装工具类,方便以后使用

    直接上源码.注释得已经很清晰了,直接调用即可. package com.liuguilin.lovewallpaper.utils; /* * Created by 火龙裸先生 on 2017/3/3 ...

  7. Eclips 快捷键设置

  8. oracle 企业管理器及无线网环境下配置方法

    注意: oracle em 的访问地址在 D:\oracle\product\11.2.0\dbhome_1\install 下的文件里. 如果在windows 安装oracle,并且在本地访问,or ...

  9. zookeeper应用 - leader选举 锁

    模拟leader选举: 1.zookeeper服务器上有一个/leader节点 2.在/leader节点下创建短暂顺序节点/leader/lock-xxxxxxx 3.获取/leader的所有子节点并 ...

  10. LeetCode题解之 two sum 问题

    1.题目描述 2.题目分析 考虑使用hashMap的方式将数组中的每个元素和下表对应存储起来,然后遍历数组,计算target 和 数组中每个元素的差值,在hashMap中寻找,一直到找到最后一对. 3 ...