1.简单查询
–查询工资超过3000的员工
select * from emp where sal>3000;
–查询工资大于1500且职位是clerk的员工
select * from emp where sal>1500 and job='ClERK';
–查询工资大于1500 或者工资不等于7566的员工
select * from emp where sal>1500 or sal != 7566;
–查询工资小于1600的员工
select * from emp where sal<=1600;
2.结合逻辑运算符使用 and or
and 优先级大于or and(连接的两个条件都要满足) or(连接的两个条件有一个满足)
先看下使用or进行查询sql
–查询emp表工资大于2000或者编号在2000——6000之间的员工
select * from emp where sal>2000 or empno>2000 and empno<6000;
再看使用and查询sql
–查询emp表工资大于3000并且编号大于6000的员工
select * from emp where sal>3000 and empno>6000;
3.结合in not in使用
in 在什么什么里面 not in 不在什么什么里面
–查询工资属于1500 3000 1400 员工信息
select * from emp where sal in (1500,3000,1400);
not in 的实例:
–查询工资不属于3000 5000 800 的员工信息
select * from emp where sal not in (300,5000,800);
4.结合between…and…使用
between … and 在什么什么之间
–查询工资在1500-3000的员工信息
select * from emp where sal between 1500 and 3000;
–查询工资在1500-3000的员工信息且职位是SALESAMAN的员工
select * from emp where sal between 1500 and 3000 and job='SALESMAN';
5.模糊查找
%代表通配符。可以匹配多个字符,_ 表示的是匹配一个字符
–查询emp表中的名字包含A的员工信息
select * from emp where ename like '%A%';
–查询mgr编号中中以7开头以8结尾的字符的员工信息
select * from emp where mgr like '7__8';
6.排序查找
order by 是通过什么来排序,默认是升序排列,也可以使用asc, 降序是desc 。
–通过sal排序 默认升序
select * from emp order by sal;
通过sal排序,默认升序
–通过sal排序 降序排序
select * from emp order by sal desc;
使用distinct去除重复值
–将job中的salesman进行除去重复
select distinct job from emp;
–将ename去除重复值
select distinct ename from emp;
去除重复ename值成功,去除就是不显示重复的名字的意思。别把去重理解的太高大上哦~
在演示一个去重失败的例子:
–将job和ename去重,失败,只能去除一个列名
select distinct job,ename from emp;
7.空值处理
is null 空值
–查询mgr的值为null的员工信息
select * from emp where mgr is null;
is not null不是空值
–查询sal不是null的员工信息
select * from emp where sal is not null;
注意:查询空值是is null 而不是 = null
8.结合any和all的使用
–all 表示所有条件都要满足 套路:大于最大值,就满足所有
–all 表示所有条件都要满足
–any 表示满足任意一个
select * from emp where sal>=all(1500,3000,1000); --大于最大值就满足所有值
select * from emp where sal>=3000; --等于于>=all(1500,3000,1000)
–any 表示满足任意一个 套路:大于最小值,就满足所有
select * from emp where sal>=any(1500,3000,1000); --大于最小值,就满足所有
等价于:
select * from emp where sal>=1000; --等价于 >=1000