The filter conditions are:
- 比较:=、>、<、>=、<=、!=、<>、between and
- 逻辑:and、or、not、union、union all、intersect、minus
- null:is null、is not null、not is null
- Fuzzy query: like:% _
- in、exists
- Subquery
Statement:
where 条件
Implementation process:
from→where→select
1. Comparison conditions
1、=
-- 查询部门编号为30的员工
select * from emp where deptno = 30
operation result |
---|
2、>
-- 查询工资大于1500的员工
select * from emp where sal > 1500
operation result |
---|
3、<
-- 查询工资小于1500的员工
select * from emp where sal < 1500
operation result |
---|
4、>=
-- 查询工资大于等于1600的员工
select * from emp where sal >= 1600
operation result |
---|
5、<=
-- 查询工资小于等于1600的员工
select * from emp where sal <= 1600
operation result |
---|
6、!=|<>
!=
And<>
the effect is the same
-- 查询不是30部门的员工 !=
select * from emp where deptno != 30
-- 查询不是30部门的员工 <>
select * from emp where deptno <> 30
operation result |
---|
7、between and
between and
Is greater than or equal to a certain value, and less than or equal to a certain value
-- 大于等于并且小于等于
select * from emp where sal between 1250 and 1500
operation result |
---|
Two, logical operations
1、and
And, satisfy the left and satisfy the right
-- and
select * from emp where sal >= 1250 and sal<=1500
operation result |
---|
2、or
Or, meet the left or right
-- or
select * from emp where deptno = 20 or deptno = 30
operation result |
---|
3、not
Negate
-- 不在部门30的员工
select * from emp where not deptno = 30
operation result |
---|
4、union
Union, remove duplication, union of two result sets, sort by default rules
select * from emp where deptno = 30
union
select * from emp where deptno = 20
operation result |
---|
5、union all
Same as above, but does not remove duplicate record values and does not sort
select * from emp where mgr = 7839
union all
select * from emp where deptno = 20
operation result |
---|
6、intersect
Intersection, find duplicate records, intersection of two result sets, default sort
select * from emp where mgr = 7839
intersect
select * from emp where deptno = 20
operation result |
---|
7、minus
Difference set, remove duplicate records, difference set of two result sets, default sort
select * from emp where sal >=1500
minus
select * from emp where sal <=3000
operation result |
---|
Three, null
1、is null
Is null
-- 没有奖金的员工
select * from emp where comm is null
operation result |
---|
2、is not null|not is null
Not null,
is not null
andnot is null
consistent
-- 有奖金的员工
select * from emp where not comm is null
select * from emp where comm is not null
operation result |
---|
Four, fuzzy query
Tips: fuzzy query efficiency is low
grammar:like 通配符
1、%
%: any number of symbols, zero to any number of symbols
-- 名字中带N的员工
select * from emp where ename like '%N%'
operation result |
---|
2、_
_: one character
-- 名字的第一个字母是A的员工
select * from emp where ename like '_A%'
operation result |
---|
Five, in and exists
1、in
Equivalent to multiple
or
-- 20部门和30部门的员工
select * from emp where deptno in (20,30)
operation result |
---|
2、exists
If exists has a result, the outer layer retains the data, and the inner query uses the external data
select * from emp e1 where exists(select * from emp e2 where e1.deptno = e2.deptno)
operation result |
---|
Six, subqueries
-- 有员工的部门的部门名称
select dname from dept where deptno in (select distinct deptno from emp)
operation result |
---|