1、null 问题: 不能比较 is null is not null not is null
select ename,sal,comm from emp where
comm is null;
comm is not null;
not (comm is null);
2、distinct去重
select distinct deptno from emp; //查询 所有的雇员 存在的部门编号
select distinct mgr from emp; //查询 上级、部门经理人的编号
3、
--"" 原样输出: 区分大小 包含空格
--'' 字符串
select ename as 雇员名称,sal as "SAl",comm "co mm" from emp;
4、
--any some all
--any 任意一个 >=any() 大于等于最小值 <=any() 最大值
--some 与any相同
select ename,job,sal from emp where sal>=any(800,1000,3000);
--all 所有 >=all() 大于等于最大值
select ename,job,sal from emp where sal>=all(800,1000,3000);
5、
-- null 不参与运算 注意顺序
--存在记录先求出800,再比较null
select ename,job,sal from emp where sal>=all(800,null);
--不存在记录 先比较null,没有记录
select ename,job,sal from emp where sal>=all(null,800);
6、
集合:and:交集 or:并集 not :补集
--intersect==and
select ename,deptno,sal from emp where sal=800 and deptno =20;
select ename,deptno,sal from emp where sal=800
Intersect
select ename,deptno,sal from emp where deptno =20;
--union==or
select ename,sal from emp where sal =800 or sal =3000;
select ename,sal from emp where sal =800 union
select ename,sal from emp where sal =3000;
--not ==!
select * from emp where deptno!=20;
select * from emp where not deptno=20;
--union与union all的区别 (union 去除重复)(union all 累加)
select * from emp where sal =800 union
select * from emp where deptno=20;
select * from emp where sal =800 union all
select * from emp where deptno=20;
--minus 差集 保留不重复数据
select * from emp where deptno=20
minus
select * from emp where sal =800 ;
7、模糊查询
select * from emp where ename like '%A%';(包含 A字符的 雇员信息)
select * from emp where ename like '_A%';(第二个字符为 A 的 雇员信息)
(添加记录)
insert into emp(empno,ename,deptno,sal) values(1000,'t_est%test',20,9999);
commit;
select * from emp;
select * from emp where ename like '%y%%' escape 'y';(雇员姓名中包含 %字符的 雇员信息)
8、
1)、
in(list),not in(list):多个定值 查询
select * from emp where sal in(800,1000,3000);
select * from emp where deptno in (select deptno from dept where dname='SALES');
2)、
exists(sub-query):难点 只要条件为真|存在记录,即返回记录
while(外层记录){
while(内层记录){
if(没有条件为true |符合内层与外层的条件){
syso();
break;
}
}
}
3)、
in 即使已经找到记录,后面的查找还会继续
while(emp 符合条件的记录|所有的记录){
if(sal ==800){ syso(); }
if(sal ==1000){ syso();}
if(sal=3000){ syso(); }
}
select ename, deptno from emp e where e.sal>2000 and exists (select deptno from dept d where deptno=20 and e.deptno != d.deptno);
select ename, deptno from emp e where e.sal>2000 and exists (select deptno from dept d where dname=upper('sales') and e.deptno = d.deptno);
4)、
(where 中不能使用 当前层的 select 里面的别名或者伪列)
select * from (select ename, sal s from emp) where s>2000;(查询工资大于2000的雇员信息)
年薪==(sal+nvl(comm,0))*12
查询年薪 大于30000的雇员名称
select * from (select ename,(sal+nvl(comm,0))*12 income from emp ) where income>30000;
9、排序
order by 可以使用别名和伪列
1、升序 默认 asc 、 降序 desc
2、多个字段排序 ,
3、null 问题 默认:升序 在后 ,降序在前
1)、手动控制 : null -->-1
2)、程序:
1)、查询所有的雇员信息,按工资升序显示
select * from emp order by sal asc;
2)、查询所有的雇员信息,按工资降序显示 ,工资相同 ,按佣金升序
select * from emp order by sal desc , comm asc;
10、总结
1)、三范式: 设置主键、主键依赖、消除传递依赖
2)、SQL分类: DML( select insert update delete )
DDL(create alter drop rename truncate)
DCL(grant revoke commit rollback)
3)、DML 之select
select from where order by
1)、解析: from where select order by
2)、exists