-- where 过滤条件
select last_name,hire_date from employees where to_char(hire_date, 'yyyy-mm-dd')='1994-06-07';
LAST_NAME HIRE_DATE ------------------------- --------- Higgins 07-JUN-94 Gietz 07-JUN-94 Mavris 07-JUN-94 Baer 07-JUN-94
不等于 <> !=
等于 =
select last_name,hire_date ,salary from employees where salary>=4000 and salary<=7000;
LAST_NAME HIRE_DATE SALARY ------------------------- --------- ---------- Ernst 21-MAY-91 6000 Lorentz 07-FEB-99 4200 Mourgos 16-NOV-99 5800 Popp 07-DEC-99 6900
其它比较运算
操作符 | 含义 |
between...and... | 在两个值之间 包含边界 |
in(set) | 等于值列表中的一个 |
like | 模糊查询 |
is null | 空值 |
--between...and...
-- 也是21条与上面的相同
select last_name,hire_date ,salary from employees where salary between 4000 and 7000;
LAST_NAME HIRE_DATE SALARY ------------------------- --------- ---------- Ernst 21-MAY-91 6000 Lorentz 07-FEB-99 4200 Mourgos 16-NOV-99 5800 Grant 24-MAY-99 7000
--in(set)
select last_name,department_id,salary from employees where department_id = 90 or department_id = 80 or department_id = 70;
select last_name,department_id,salary from employees where department_id in(70,80,90);
LAST_NAME DEPARTMENT_ID SALARY ------------------------- ------------- ---------- King 90 24000 Kochhar 90 17000 De Haan 90 17000 Zlotkey 80 10500
--like
--员工中名字含有 a 的员工有哪些
select last_name,department_id,salary from employees where last_name like '%a%';
LAST_NAME DEPARTMENT_ID SALARY ------------------------- ------------- ---------- Kochhar 90 17000 De Haan 90 17000 Rajs 50 3500 Davies 50 3100
--员工中名字末位是 a 的员工有哪些
select last_name,department_id,salary from employees where last_name like '%a';
LAST_NAME DEPARTMENT_ID SALARY ------------------------- ------------- ---------- Baida 30 2900 Banda 80 6200 Pataballa 60 4800 Sciarra 100 7700
--员工中名字第二位是a 的员工有哪些
select last_name,department_id,salary from employees where last_name like '_a%';
LAST_NAME DEPARTMENT_ID SALARY ------------------------- ------------- ---------- Rajs 50 3500 Davies 50 3100 Matos 50 2600 Vargas 50 2500
查询名字本身就带有下划线的名字:
先修改名字:update employees set last_name = 'Wha_len' where last_name = 'Whalen';
-- escape作用: 让_不在表示一个字符的含义
select last_name,department_id,salary from employees where last_name like '%\_%' escape '\';
LAST_NAME DEPARTMENT_ID SALARY ------------------------- ------------- ---------- Wha_len 10 4400
--is null
select last_name,department_id,salary ,commission_pct from employees where commission_pct is null;
LAST_NAME DEPARTMENT_ID SALARY COMMISSION_PCT ------------------------- ------------- ---------- -------------- King 90 24000 Kochhar 90 17000 De Haan 90 17000 Hunold 60 9000
--非空的为 is not null
select last_name,department_id,salary ,commission_pct from employees where commission_pct is not null;
逻辑运算: and or not
select last_name,department_id,salary ,commission_pct from employees where department_id = 80 and salary<7000;
LAST_NAME DEPARTMENT_ID SALARY COMMISSION_PCT ------------------------- ------------- ---------- -------------- Lee 80 6800 0.1 Ande 80 6400 0.1 Banda 80 6200 0.1 Kumar 80 6100 0.1 Johnson 80 6200 0.1
---排序 order by 默认为升asc 降desc 子句在select 语句的结尾
select last_name,department_id,salary ,commission_pct from employees where department_id = 80 order by salary desc;
LAST_NAME DEPARTMENT_ID SALARY COMMISSION_PCT ------------------------- ------------- ---------- -------------- Russell 80 14000 0.4 Partners 80 13500 0.3 Errazuriz 80 12000 0.3 Ozer 80 11500 0.25
--工资一样再按名字来排序
select last_name,department_id,salary ,commission_pct from employees order by salary desc,last_name asc;
LAST_NAME DEPARTMENT_ID SALARY COMMISSION_PCT ------------------------- ------------- ---------- -------------- King 90 24000 De Haan 90 17000 Kochhar 90 17000 Russell 80 14000 0.4 Partners 80 13500 0.3 Hartstein 20 13000 Errazuriz 80 12000 0.3 Greenberg 100 12000 Higgins 110 12000
--也可使用别名来排序
select last_name,department_id,salary ,12*salary annual_sal from employees order by annual_sal desc;
LAST_NAME DEPARTMENT_ID SALARY COMMISSION_PCT ------------------------- ------------- ---------- -------------- King 90 24000 De Haan 90 17000 Kochhar 90 17000 Russell 80 14000 0.4
练习 :
1.选择雇用时间在1998-02-01到1998-05-01之间的员工姓名,job_id和雇佣时间
select last_name, job_id,hire_date from employees where to_char(hire_date,'yyyy-mm-dd') between '1998-02-01' and '1998-05-01';
LAST_NAME JOB_ID HIRE_DATE ------------------------- ---------- --------- Matos ST_CLERK 15-MAR-98 Taylor SA_REP 24-MAR-98 Urman FI_ACCOUNT 07-MAR-98 Seo ST_CLERK 12-FEB-98 Patel ST_CLERK 06-APR-98 Olsen SA_REP 30-MAR-98 Bloom SA_REP 23-MAR-98 Livingston SA_REP 23-APR-98 Fleaur SH_CLERK 23-FEB-98 Walsh SH_CLERK 24-APR-98 Pataballa IT_PROG 05-FEB-98 选定了 11 行
2.选择在1994年雇佣的员工的姓名和雇佣时间
select last_name,hire_date from employee where to_char(hire_date,'yyyy') = '1994';
LAST_NAME HIRE_DATE ------------------------- --------- Raphaely 07-DEC-94 Higgins 07-JUN-94 Gietz 07-JUN-94 Mavris 07-JUN-94 Baer 07-JUN-94 Greenberg 17-AUG-94 Faviet 16-AUG-94 选定了 7 行
3.选择姓名中有a和e的员工姓名
select last_name from employees where last_name like '%a%e%' or last_name like '%e%a% ';
LAST_NAME ------------------------- Davies Wha_len Hartstein Raphaely Colmenares Nayer Markle Philtanker Patel Partners Bates Gates Baer Faviet 选定了 14 行
来源:https://www.cnblogs.com/afangfang/p/12539603.html