Oracle SQL查询(全查询、部分查询、条件查询、模糊查询、子查询)

SQL查询

SQL查询主要分为全查询部分查询条件查询模糊查询等。

数据声明

1.员工信息表

包含:员工编号、员工姓名、岗位工作、上级领导编号、入职日期、工资、奖金、部门编号。
在这里插入图片描述

2.部门信息表

包含:部门编号、部门名称、部门地址。
在这里插入图片描述

一、全查询

全查询可以查询表中所有数据字段,返回emp表中存储的所有信息。

1. select * from emp;
2. select emp.* from emp;
3. select e.* from emp e;

二、部分查询

根据自生需求,按需查找表中对应字段的数据。
例如查询emp员工表中员工的编号、姓名、工作字段信息。

  select empno,ename,job from emp;
  select e.empno,e.ename,e.job from emp e;

三、条件查询

在进行表数据查询时,添加相应的限定条件,对查询的信息进行过滤,以达到更加准确的结果。
语法: 表名后面+where+限定条件(and或者or)。

1.算术运算符(+、 -、 *、 /)-条件查询

1.select * from emp where sal>500+2000;
2.select * from emp where (sal-500)>2000;
3.select ename,job,(sal*12) year_sal from emp where sal>2000;
4.select ename,job,(sal/22) day_sal from emp where sal>2000;

2.关系运算符(=、>、>=、<、<=、!=、<>)-条件查询

1.select * from emp where sal<=2000;
2.select * from emp where ename='scott';
3.select * from emp where sal=2800;
4.select * from emp where sal<>2000;
5.select * from emp where sal!=2000;

3.逻辑运算(and、or、not)执行顺序(not > and > or)-条件查询

1.select * from emp where sal<2000 and sal>1500;
2.select * from emp where sal<2000 or comm>300;
3.select * from emp where not sal<2000;
4.select * from emp where not deptno=20 and deptno=30;--结果(30)
5.select * from emp where not (deptno=20 and deptno=30);--判断条件内部为假,not后为真==全查
6.select * from emp where not (deptno=20 or deptno=30);--结果(10)
7.select * from emp where not deptno=20 and deptno=30 or deptno=10;--结果(10,30)
8.select * from emp where not deptno=20 and deptno=30 or deptno=10 and deptno=20;--结果(30)
9.select * from emp where not deptno=20 and deptno=30 or (deptno=10 and deptno=20);--结果(30)
10.select * from emp where deptno=20 and deptno=30 or deptno=10;--前面为假,结果为(10)
11.select * from emp where deptno=20 or deptno=30 and deptno=10;--后面为假,结果为(20)    

4.空值null(is null、is not null)-空值查询

1.select * from emp where comm is null;
2.select * from emp where comm is not null;

5.区间查询(between and)等价于 >= and <=

语法:between 小值 and 大值

1.select * from emp where sal between 2500 and 3000;
2.select * from emp where sal>=2500 and sal<=3000;

6.IN()查询,包含查询

语法:IN(值1,值2,值3…)
等价于:-or-
等价于:any(值1,值2,,)

1.select * from emp where job in('ANALYST','MANAGER','CLERK');
2.select * from emp where job='ANALYST' or job='MANAGER' or job='CLERK';
3.select * from emp where job=any('ANALYST','MANAGER','CLERK');

7.NOT IN()查询

语法:IN(值1,值2,值3…)
等价于: !=值1 and !=值2 and!=值
等价于:!=all()

1.select * from emp where job not in('ANALYST','MANAGER','CLERK');
2.select * from emp where job!='ANALYST' and job!='MANAGER' and job!='CLERK';
3.select * from emp where job!=all('ANALYST','MANAGER','CLERK');

四、模糊查询 LIKE

语法: where 字段 like ‘通配符和数据’(有多个用and或or)
1.字段值可以是字符型或者数字类型,不可以是日期类型
2.通配符:
_ 表示此处位置占据具体的一位;
% 表示0位或者多位。

select * from emp where ename like '_A%'; 
select * from emp where ename like '_A%' or ename like '_C%';
select * from emp where ename like '%M%S%';
select * from emp where empno like '%77%';
--查询出员工名称包含%的员工数据信息  用到转义符 \
select * from emp where ename like '%%%'; --查询条件错误
select * from emp where ename like '%\%%' escape '\';  --指明转义符为'\',转义的字符为 %
--日期转换成普通字符串,匹配查询
select e.* from emp e where to_char(HIREDATE,'YYYYMMDD') like '1981%';
select * from emp where ename like '_A%';
--查询员工名字是4个字母的员工信息
select * from emp where ename like '____';
select * from emp where length(ename)=4;

五、子查询

注意:

  1. 子查询的select语句不要带分号
  2. 子查询的select语句用括号括起来

1.子查询结果当条件用

  select *
  from emp e
  where deptno=(select deptno from emp where ename='SCOTT') --将查询结果部门编号当成条件使用
  and e.ename!='SCOTT';

在这里插入图片描述

2.子查询结果当表用

  SELECT D.*,F.*  
  FROM DEPT D
  inner join (SELECT * FROM EMP E WHERE E.ENAME = 'SCOTT') F --当成临时表用,进行表关联
  on D.DEPTNO = F.DEPTNO;

在这里插入图片描述

in()和 exists()

  • exists() 存在为真 , 逻辑:先执行主查询,然后执行子查询
  • in() 逻辑:先执行子查询,然后执行主查询
  • not exists() 不存在则为真
例:查询SMITH上班地点
--用in()
select f.loc from dept f
where f.deptno in(select deptno from emp where ename='SMITH'); --DALLAS
--用exists()
select f.loc from dept f --主查询
where exists(select 1 from emp e where e.ename='SMITH' and e.deptno=f.deptno);  --DALLAS
查出dept表有的但是emp表却没有的部门编号来
--not exists()
select f.deptno from dept f
where not exists(select 1 from emp e where e.deptno=f.deptno); --40
--exists()   --错误方式
select f.deptno from dept f
where exists(select 1 from emp e where e.deptno!=f.deptno);--不能用!=;

总结一下:

  1. 在条件不明确时候用到子查询
  2. 子查询的结果,可以当做比较的条件来使用
  3. 子查询的结果,也可以当做一张新的临时表来使用
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Oracle SQL 中,子查询是一个查询嵌套在另一个查询中的查询子查询可以用于过滤数据、计算聚合值、连接表等操作。为了优化子查询的性能,可以考虑以下几点: 1. 使用合适的子查询类型:Oracle 提供了多种子查询类型,包括标量子查询、单行子查询、多行子查询等。选择最适合需求的子查询类型可以提高查询效率。 2. 使用 EXISTS 替代 IN 子查询:在某些情况下,使用 EXISTS 关键字替代 IN 子查询可以提高查询性能。EXISTS 只需要判断是否存在匹配记录,而不需要返回具体的匹配记录。 3. 使用合适的连接方式:当使用子查询与外部查询进行连接时,可以考虑使用合适的连接方式(如 INNER JOIN、LEFT JOIN 等)。根据数据量和索引情况选择合适的连接方式可以提高查询性能。 4. 使用合适的索引:为了加快子查询的执行速度,可以通过创建合适的索引来优化查询。根据子查询中涉及的列,创建适当的索引可以减少数据访问的次数。 5. 注意子查询的返回结果集大小:如果子查询返回的结果集较大,在性能方面可能会有一定的影响。可以考虑优化子查询或使用其他方法来处理大数据量的情况。 6. 使用合适的限制条件:在子查询中使用合适的限制条件可以缩小结果集的大小,从而提高查询效率。 7. 避免多层嵌套的子查询:多层嵌套的子查询可能会导致性能下降,可以尝试重构查询语句,减少子查询的嵌套层数。 8. 使用临时表或内联视图:在某些情况下,将子查询的结果保存到临时表或使用内联视图可以提高查询性能。 以上是一些常见的优化子查询的方法,具体的优化策略还需要根据实际情况进行分析和调整。同时,可以使用 Oracle 提供的性能调优工具(如 Explain Plan、SQL Trace 等)来分析查询执行计划和性能瓶颈,帮助进一步优化子查询的性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值