sql语句

/distinct 去重 查询职位名称
select distinct e.job
from emp;

/where 表示查询条件
/关系运算符(=,!=/<>,<,>,<=,>=)
-- where 子句

-- 把部分10的雇员查询出来
select *
from emp
where deptno = 10;

-- 把名称为smith的雇员
select e.*
from emp e
where e.ename = 'SMITH';

-- 查询底薪大于等于1000的员工
select e.*
from emp e
where e.sal >= 1000;

select e.*
from emp e
where e.sal <> 800

/any/some/all(list)
/any/some(list)(满足list列表中的任意一个条件)any和some用法相同

--查询部门编号是10或者20的雇员
select *
from emp
where emp.deptno=any(10,20);

all(list)(满足list列表的所有条件)
-- 查询薪资大于1000
select e.*
from emp e
where e.sal > all(1000,800);

/null
/-- null/not null
-- 查询没有津贴的雇员
select e.*
from emp e
where e.comm is null

select e.*
from emp e
where e.comm is not null

/between x and y
/表示一个值位于[x,y]区间,x/y 一般都是数字
--查询薪资在1000到2000之间的雇员
select e.*
from emp e
where e.sal between 1000 and 2000;

/in/not in list
/表示字段是否在list列表中
-- 查询部门编号是10和20的员工
select e.*
from emp e
where e.deptno in(10,20);
--查询部门编号不是10和20的员工
select e.*
from emp e
where e.depthno not in(10,20);
-- 查询薪资是1000,2000,5000的员工
select e.*
from emp e
where e.sal in (1000,2000,5000);

/ like 关键字用于模糊查询,其中
/ %:表示任意字符出现多次(含0次),
/ _:表示任意字符出现1次。
/ escape(‘x’) 表示指定转义字符为x,一般指定为\

-- 查询名字是c开头的雇员

select e.*
from emp e
where e.ename like 'c%';

-- 查询名字中第二个字母是M的雇员
select e.*
from emp e
where e.ename like '_M%'

-- 查询名字中含有M的雇员
select e.*
from emp e
where e.ename like '%M%';

-- 查询名字中含有%的雇员
select e.*
from emp e
where e.ename like '%\%%' escape('\');

/ where 后面的条件可以跟多个通过and 或者 or 连接
/ and:且、并且
/ or: 或、或者

-- 查询部门10且薪资大于等2000的雇员
select e.*
from emp e
where e.deptno = 10 and e.sal >= 2000;

-- 查询名字中含M且薪资大于1000的雇员
select e.*
from emp e
where e.ename like '%M%' and e.sal > 1000

-- 查询部门在10或20的雇员
select e.*
from emp e
where e.deptno = 10 or e.deptno = 20

/ where中 and和or的执行效率
/ and 表示且,条件越多,检索的数据量越来越少,and:  把检索结果较少的条件放到后面
/ or 表示或,条件越多,检索的数据量越来越多, or:把检索结果较多的条件放后面
/ where 条件的执行顺序从后向前

/ 当and和or同时存在时,and先执行。

--使用in查询部门名称为 SALES 和 RESEARCH 的雇员姓名、工资、部门编号
-- 其中部门名称位于dept,雇员信息位于emp表
select e.ename,e.sal,e.deptno
from emp e
where e.deptno in
(
select d.deptno
from dept d
where d.dname = 'SALES' or d.dname = 'RESEARCH'
);

 

/*

注意:很多记录中的comm是null,表示不确定的值,经常四则运算后的值也不确定。

当遇到字段时null时,可以通过nvl函数把null转化便于运算的类型。

*/

-- nvl函数优化
select e.ename,e.sal+nvl(e.comm,0) "收入",e.deptno
from emp e;

posted @ 2019-05-13 22:01  麻烦关下灯  阅读(183)  评论(0编辑  收藏  举报