oracle 基础第一天总结

SELECT

select基础语法:

--查询语句

--select *|数据,数据... from 数据源

--DML  DQL数据查询语言

--select *(全部列) from 数据源

select * from emp; --查询emp表中的全部数据的所有列

--查询指定列

--select 字段名1,字段名2,... from 数据源

select empno,ename,sal from emp;  --查询emp表中所有数据的指定列empno,ename,sal

SELECT EMPNO FROM EMP; --命令不区分大小写,字符串内容区分大小写

--伪列 (整数,表达式,字符串): 表中不存在的字段,但是可查询的内容

--整数

select empno,ename,sal,1 from emp;

--表达式

select empno,ename,sal,sal+100,123*456 from emp;

--字符串 '字符'

select '哈哈' from emp;

select empno,ename,deptno,'娃哈哈公司' from emp;

--字符串连接符 ||

select 'yjxxt-'||ename from emp;

--字段别名: 为查询的结果集中的列起别名(名字默认为字段名或者伪列的值)

--select 数据 (as) 别名,数据 别名,数据 别名... from 数据源

select ename as 员工姓名, sal 员工薪资, 202 "教室    编号" from emp;

--当别名名字内容比较特别,存在特殊符号,空格,小写名称...  可以在名字的前后添加一对""--->""中的内容原封不动显示

select ename "name" from emp;

--表的别名: select 数据 from 表名 别名  

--表的别名不能添加as

select e.ename,e.deptno from emp e;

--查询公司所有的部门编号

select deptno from dept;

--查询有员工存在的部门编号

select deptno from emp;

--去重: distinct 对结果集中完全相同的数据进行去重

select distinct deptno from emp;

--查询语句中查询数据的位置: *与字段名不能同时存在

select e.*,e.ename,e.sal from emp e;

--虚表 dual

select distinct 123*456 from emp;

select sysdate from dual;

--null 一个字段没有赋值默认null

--为员工奖金涨薪100

--null与数字运算结果为null

select empno,ename,comm,comm+100 from emp;

--null值与字符串拼接,结果字符串

select empno,ename,comm,comm||100 from emp;

--nvl(值1,值2) 当值1为null,结果为值2,当 值1不为null,结果为值1

select empno,ename,comm,nvl(comm,0)+100 from emp;

--查询语句: select 数据 from 数据源  查询一张表中的所有数据的指定字段值

--条件查询: select 数据字段 from 数据源 where 行过滤条件    --> 满足行过滤条件where的数据被保留在结果集中,只保留指定字段的值select

--执行过程: from -->where-->select

a)、= 、 >、 <、 >=、 <=、 !=、 <>、 between and

b)、and 、or、 not

c)、集合函数 union、  union all、 intersect 、minus

c)、null  :is null、  is not null、  --not is null

d)、like :模糊查询 % _ escape('单个字符')

-- 查询20部门的员工信息

--查询的数据 : 员工信息 *

--数据的来源 : 员工表 emp

--条件 : 20部门  deptno=20

select * from emp where deptno=20;

-- 查询工资大于1000的员工的姓名 工作岗位  工资   所属部门编号

--查询的数据 : ename, job,sal,deptno  

--数据的来源 : 员工表 emp

--条件 : 工资大于1000 sal>1000

select ename,job,sal,deptno from emp where sal>1000;

-- 查询不在20部门工作的员工信息

select * from emp where deptno!=20;

select * from emp where deptno<>20;

select * from emp where not deptno=20;

-- 查询员工的年薪大于20000的 员工名称、岗位 年薪

select empno,ename,job,sal,sal*12 from emp where sal*12>20000;

--select后的别名在where中不能使用,因为执行流程问题,先执行from->where->select

--select empno,ename,job,sal,sal*12 result from emp where result>20000;

--先查询后判断

--1)查询出所有员工的信息,包含年薪

select empno,ename,job,sal,sal*12 result from emp;

--2)对已经查询出来(1)保留的结果集)当做数据源判断年薪>20000

select * from (select empno,ename,job,sal,sal*12 result from emp) where result>20000;

-- 假设现在 900,1000,1100,查询工资比我们三个人都高的那些员工的信息

select * from emp where sal > all(900,1000,1500);

-- 查询工资比我们三个人都低的那些员工的信息

select * from emp where sal < all(900,1000,1500);

-- 查询比我们中随便一个工资高的员工信息

select * from emp where sal > any(900,1000,1500);

-- 查询 工种为’SALESMAN’的员工信息 (注意 内容区分大小写)

select * from emp where job = 'SALESMAN';

-- -检索 工资 2000, 3000员工名称 岗位 工资

select * from emp where sal=1500 or sal=3000;

-- 工资在2000到3000之间的员工信息

select * from emp where sal>=2000 and sal<=3000;

--between and 相当于>= <=

select * from emp where sal between 1500 and 3000;  

---查询 岗位 为 CLERK 且部门编号为 20的员工名称 部门编号,工资

select ename,deptno,sal,job from emp where job='CLERK' and deptno=20;

-- 查询 岗位 为 CLERK 或部门编号为 20的员工名称 部门编号,工资

select ename,deptno,sal,job from emp where job='CLERK' or deptno=20;

-- 查询 岗位 不是 CLERK 员工名称 部门编号,工资

select ename,deptno,sal,job from emp where job!='CLERK';

select ename,deptno,sal,job from emp where not job='CLERK';

select ename,deptno,sal,job from emp where job<>'CLERK';

-- 查询 岗位 不为 CLERK 并且部门编号不为 20的员工名称 部门编号,工资

select * from emp where job!='CLERK' and deptno!=20;

select * from emp where not(job='CLERK' and deptno=20);

-- 存在奖金的员工名称

select ename,sal,comm from emp where comm is not null;

select ename,sal,comm from emp where not comm  is null;

-- 不存在奖金的员工名称

select ename,sal,comm from emp where  comm is null;

-- 查询工资大于1500 或 含有佣金的人员姓名

select * from emp where sal>1500 or comm is not null;

--集合函数 union(去重)、 union all、 intersect 、minus

Union,并集(去重) 对两个结果集进行并集操作,不包括重复行同时进行默认规则的排序;

Union All,全集(不去重) 对两个结果集进行并集操作,包括重复行,不进行排序 ;

Intersect,交集(找出重复) 对两个结果集进行交集操作,不包括重复行,同时进行默认规则的排

序;

Minus,差集(减去重复) 对两个结果集进行差操作,不包括重复行,同时进行默认规则的排序

select * from emp where sal>1500

union

select * from emp where comm is not null;

-- 查询显示不存在雇员的所有部门号。

select deptno from dept

Minus  --差集

select distinct deptno from emp;

--like :模糊查询 % _ escape('单个字符')

-- % 任意个任意字符

-- _ 一个任意字符

-- 查询员工姓名中包含字符A的员工信息

select * from emp where ename like '%A%';

-- 查询员工姓名中第二个字母为A的员工信息

select * from emp where ename like '_A%';

-- 查询名字 中包含%的员工信息

--插入2条数据,员工名称中带有%

insert into emp(empno,ename,sal) values(1000,'t_%test',8989);

insert into emp(empno,ename,sal) values(1200,'t_tes%t',8000);

--在对一张表中的数据做增删改操作的时候会默认开启事务,commit提交  rollback回滚

select * from emp where ename like '%S%%' escape('S');

--删除表中的数据

delete from emp where ename like '%S%%' escape('S');

-- 查询工资为 1500, 2000, 2500, 5000的员工的信息

select * from emp where sal=1500 or sal=2000 or sal=2500 or sal=5000;

--in(值列表) 判断指定的字段的值是否与in后面的值列表中的某个值相等,相等就满足

select * from emp where sal in (1500, 2000, 2500, 5000);

-- 部门名称为 SALES 或 ACCOUNTING 的雇员信息

--1)查询到部门名称为 SALES 或 ACCOUNTING的部门编号

select deptno from dept where dname='SALES' or dname='ACCOUNTING';

select deptno from dept where dname in('SALES','ACCOUNTING');

--2)在以上两个部门编号的员工信息

select * from emp where deptno in(10,30);

--子查询 : 查询语句嵌套查询语句

select *

  from emp

 where deptno in

       (select deptno from dept where dname in ('SALES', 'ACCOUNTING'));

-- 查询工资等级为 2的员工信息

--查询2等级的最低薪

select losal from salgrade where grade = 2;

--查询2等级的最高薪

select hisal from salgrade where grade = 2;

select *

  from emp

 where sal between (select losal from salgrade where grade = 2) and

       (select hisal from salgrade where grade = 2);

--查询1500薪资的等级

select grade from salgrade where 1500 between losal and hisal;

-- 查询 销售部(SALES) 中 工资大于1500的员工信息

--查询工资>1500的员工信息

select * from emp where sal>1500;

select * from emp where deptno = (select deptno from dept where dname = 'SALES') and sal>1500;

-- 查询工资比SMITH高的与SMITH同一部门的员工信息

--SMITH的工资

select sal from emp where ename='SMITH';

--SMITH的部门编号

select deptno from emp where ename='SMITH';

select * from emp where sal>(SMITH的薪资) and deptno = (SMITH的部门编号);

select * from emp where sal>(select sal from emp where ename='SMITH') and deptno = (select deptno from emp where ename='SMITH');

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值