条件查询

条件查询

where字句用来过滤查询的数据,它对字面量大小写是敏感

语法:
select 列名1,列名2...
from 表名
where 筛选的条件;

1.比较运算符
>,<,=,<=,>=,!=(^=,<>)


--查询在41部门的员工名字,工资
select
first_name,salary
from

s_emp

where
dept_id=41
;

--查出工资高于1500的员工的信息;
select * from s_emp where salary>1500;

--找出工资大于1200的员工的全名、工资、职位
select first_name||' '||last_name 全名,salary 工资,title 职位
from s_emp
where salary >1200;


-----------------------------------------------------------------------

2.逻辑运算符
and,or

--查出41部门中工资高于1200的员工名字,工资;
select first_name, salary ,dept_id
from s_emp
where dept_id = 41 and salary > 1200;


--查出41,50,42部门的员工名字,薪水;
select first_name,salary from s_emp where dept_id=41 or dept_id=42 or dept_id=50;

----------------------------------------------------------------------

3.其他运算符
in() 取多个数值,多个值使用逗号隔开
例:
deot_id in(41,42,50)相当于dept_id=41 or dept_id=42 or dept_id=50;

not in()
in(2,3,4)相当于 2or3or4

between 2 and 6 在指定的范围之内,是全闭空间;相当于>=2 and <=6
dept_id>2 and dept_id<6 非全闭空间

is null
is not null

模糊查询:like
通配符:rose
_通配任意单个字符
%通配任意多个字符


--查出在41,42,50部门的员工名字,薪水;
select first_name,salary from s_emp where dept_id in(41,42,50);

--找出工资在1200到1500之间的员工名字;(全闭和不全闭)
全闭:
select first_name,salary
from s_emp
where salary between 1200 and 1500;
非全闭:
select first_name,salary
from s_emp
where salary >1200 and salary <1500;

--找出工资大于1500并且没有提成的员工;
select * from s_emp
where salary > 1500 and commission_pct is null;

注意:查询数据时条件是否为null,我们使用关键字is null 或者is not null ,千万不能使用=

--查询名字是以M打头的员工;
select * from s_emp where first_name like 'M%';

--查出姓名中第三个字母是e的员工;
select * from s_emp where first_name like '__e%';

==================================
练习:

--找出没有提成率的员工
select * from s_emp where commission_pct is null;

--找出有提成率的员工
select * from s_emp where commission_pct is not null;

--找出费用超过10000元的订单编号及支付方式
(订单表:s_ord,订单编号:id,支付方式:payment_type)

select id,payment_type from s_ord
where total >10000;

--找出工资在950(含)至1200(含)元的员工姓名、职位;

select fisrt_name,title from s_emp where salary between 950 and 1200;

================================
--找出名字中含有字母a的员工
select * from s_emp where first_name like '%a%';

--找出名字中第二个字母是a的员工
select * from s_emp where first_name like '_a%';

----------------------------------------------------------
排序字句:
select 列名1,列名2...
from 表名
where 查询条件
order by 列名 asc(升序,默认可以不写)|desc(降序)

案例:

--找出查询职位是Stock Clerk的员工全名、工资,并按照工资的降序排序
select first_name||' '||last_name 全名,salary 工资
from s_emp
where title = 'Stock Clerk'
order by salary desc;

--查询员工姓名,工资,并按照工资的降序排序
select first_name,salary from s_emp order by salary desc;

--找出员工全名,工资,职位,并按照工资降序排序;
select first_name||' '||last_name,salary,title from s_emp order by salary desc;


--查询职位中带vp(大写)的员工名字,工资,并按照工资降序排序;
select first_name,salary,title from s_emp where title like '%VP%' order by salary desc;

--查询出年薪(包含提成)低于25000的员工名称,职位,年薪,并按照年薪升序排序;
select first_name,title,salary*12*(1+nvl(commission_pct/100,0)) from s_emp where salary*12*(1+nvl(commission_pct/100,0))<25000
order by salary*12*(1+nvl(commission_pct/100,0)) asc;

原文地址:https://www.cnblogs.com/zhang12354/p/7803174.html