Oracle数据库SELECT语句的使用

SELECT语句是数据库中使用频率非常高的语句,就像进入淘宝,京东百度这样的网站往往首先做的事情就是查询;所以查询语句十分重要,需要好好学习,熟练掌握;在这里插入图片描述

SELECT的语句格式:

SELECT 查询字段 FROM 数据源 WHERE 条件语句 ORDER BY 排序字段

SELECT的具体使用:

--查询所有的员工信息
--查询的数据: * 所有的员工信息
--数据的来源: 员工表 emp
--条件:
select * from emp;
select * from dept;

SELECT多字段的查询:

--select 字段1,字段2... from 数据源;
select ename,comm from emp where comm is not null;

SELECT别名:
直接在字段后添加别名即可,中文直接添加,小写的字母需要使用" “英文双引号包裹,”"表示原样输出

--select 字段1,字段2... from 数据源;
select ename 员工姓名,comm 奖金 from emp where comm is not null;

SELECT条件查询WHERE的使用:

--条件查询 select 数据 from 数据源 where 行过滤条件;
--条件比较符: = > < >= <= != <>
--逻辑连接符:and or not
-- 查询20部门的员工信息
select * from emp where deptno = 20;
-- 查询工资大于1000的员工的姓名 工作岗位  工资   所属部门编号
--查询的数据: ename , job , sal , deptno
--数据来源: emp
--条件: 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;
---查询 岗位 为 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 job <> 'CLERK';
select ename,deptno,sal,job from emp where not job = 'CLERK';

SELECT语句处理NULL值:

--判断是不是NULL
--存在佣金|奖金的员工名称
select ename,comm from emp where comm is not null;
select ename,comm from emp where not comm is null;
--NULL值参加运算需要用到NVL();
-- 查询员工的年薪大于20000的 员工名称、岗位 年薪
--所有员工的奖金+100
select ename,job,nvl(comm,0)+100 from emp;

SELECT语句LIKE模糊查询及转义字符ESCAPE()的使用:

--like 模糊匹配  
--查询员工姓名中字符A开头员工信息
select * from emp where ename like 'A';
--查询员工姓名中包含字符A的员工信息
select * from emp where ename like '%A%';
--查询员工姓名中第二个字母为A的员工信息
select * from emp where ename like '_A';
--查询姓名字段中含有%的员工信息
select * from emp where ename like '%a%%' escape('a')

SELECT语句BETWEEN AND的使用:

-- 工资在2000到3000之间的员工信息
select * from emp where sal between 2000 and  3000; 

SELECT语句IN()和EXISTS()的使用:
IN():查询值是否在存在()内
EXISTS():查询的值是否和()内的值相同

--查询部门名称为 SALES 或 ACCOUNTING的部门编号
select deptno from dept where dname in ( 'SALES' , 'ACCOUNTING');
--员工在 ↑部门的员工信息
select * from emp where deptno in(10,30);
select *
  from emp
 where deptno in
       (select deptno from dept where dname in ('SALES', 'ACCOUNTING'));
--与有奖金存在的员工部门编号相等的员工信息
select empno, ename, sal,deptno
 from emp e1
where exists (select empno, ename, sal, comm,deptno
 from emp e2
 where comm is not null
 and e1.deptno = e2.deptno );

SELECT语句的排序ORDER BY的使用默认升序,asc升序,desc降序:

--查询 EMP 表显示获得补助的所有雇员名、工资及补助,并以工资升序和补助降序排序。
select ename,sal,comm from emp where comm is not null order by sal asc,comm desc;

SELECT语句去重DISTINCT的使用:

--查询所有部门编号,相同的保留一个
select distinct deptno from emp;
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值