重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
使用WHERE子句,将不满足条件的行过滤掉:
创新互联建站专注于企业成都营销网站建设、网站重做改版、邛崃网站定制设计、自适应品牌网站建设、html5、商城网站开发、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为邛崃等各大城市提供网站开发制作服务。
WHERE过滤的注意事项:
WHERE子句紧随FROM子句。
WHERE子句后跟条件表达式
列名、表达式、常量
比较运算符:=, <=, BETWEEN, IN, LIKE, 和 NULL
–逻辑运算符: AND, OR和 NOT
替代变量
DEFINE 和 VERIFY 命令
文字值
WHERE子句后不能跟列别名
语法如下:
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table
[WHERE condition(s)];
1、比如现在查找一下部门ID为90的部门里面有哪些员工
select last_name,department_id from employees where department_id = 90;
2、查找部门ID 为100的人员信息,如:姓名、员工编号等
select first_name,employee_id,job_id,department_id from employees where department_id = 100;
3、当条件为字符和日期要包含在单引号中。
字符大小写敏感,日期格式敏感。
默认的日期格式是 DD-MON-RR
3.1 查找last_name为Whalen的员工departmen id等信息
select last_name,job_id,department_id from employees where last_name = 'Whalen';
3.2 查找入职日期为07年12月19日的员工first_name 和部门ID
select first_name,department_id,hire_date from employees where hire_date = '19-DEC-07';
4、比较运算符
操作符 | 含义 |
= | 等于 |
> | 大于 |
>= | 大于等于 |
< | 小于 |
<= | 小于等于 |
<> | 不等于 |
BETWEEN ...AND... | 在 ....和....之间 |
IN(set) | 在这些之中 |
LIKE | 选择类似值 选择条件可以包含字符或数字 % 代表一个或多个字符 _ 代表一个字符 |
IS NULL | 为空的时候 |
4.1 查找一下工资小于2800 的员工的名字和部门id号
select last_name,department_id,salary from employees where salary < 2800 order by salary;
4.2 查找一下工资大于等于3000 的员工 的名字和job_id,并采用升序排列
select last_name,job_id,salary from employees where salary>=3000 orader by salary;
4.3 查找一下工资大于等于10000的员工姓名,并按照 xxx 's salary is 10000的格式输入
select first_name||q'['s salary is ]' || salary as "salary is 10000 of emp"
from employees
where salary=10000;
4.4 查找一下工资在8000-10000之间的员工姓名和员工id
select employee_id,first_name,salary from employees where salary between 8000 and 10000 order by salary;
4.5 查找一下工资为7000、3100、8100、9000、10000的员工名字
select first_name,salary from employees where salary in(7000,3100,8000,9000,10000);
4.6 查找员工名称为S开头的job_id
select first_name,job_id from employees where first_name like 'S%';
4.7 查找员工名称中带有s的 job_id
select first_name,job_id from employees where first_name like '%s';
4.8 查找名字第二个字母是o的员工信息
select first_name,job_id from employees where first_name like '_o%';
4.9 查找名字第三个字母为e和a的员工名字
select employee_id,last_name,salary,department_id from employees where manager_id= &mgr_num order by &order_col
4.9.1 查找manager_id 为空的名字
select first_name,manager_id from employees where manager_id is null;
4.9.2 查找到04年入职的员工姓名
select last_name,hire_date from employees where hire_date like '%04';
5、逻辑运算符
操作符 | 含义 |
AND | 逻辑并,两个条件都为“真”则返回TRUE |
OR | 逻辑或,其中一个条件为“真”则返回TRUE |
NOT | 逻辑否,如果条件为“假”则返回TRUE |
5.1、查找工资大于10000并且 job_id 含有MAN的员工信息
select first_name,employee_id,salary from employees where salary>=1000 and job_id like '%MAN%';
5.2、查找工资大于等于10000 或者job_id 含有MAN的员工 的名字和employee_id
select first_name,employee_id,job_id,salary from employees where salary>=10000 or job_id like '%MAN%';
5.3 查找job_id 不在'HR_EMP','ST_MAN','ST_CLERK' 这工作id里面的员工名字和job_id
select last_name,job_id from employees where job_id not in('HR_EMP','ST_MAN','ST_CLERK');
5.4 列出工资不在 5000-12000 范围的员工的姓名和工资
SQL >select last_name,salary from employees where salary not between 5000 and 12000 order by salary;
LAST_NAME SALARY
------------------------- ----------
Olson2100
Philtanker2200
Markle2200
Landry2400
Gee 2400
Vargas2500
Patel2500
Colmenares2500
Marlow2500
Sullivan 2500
Perkins 2500
OConnell 2600
Grant2600
Matos2600
Himuro2600
Mikkilineni2700
Seo 2700
Atkinson 2800
Geoni2800
Tobias2800
Jones2800
Rogers2900
Baida2900
Gates2900
Feeney3000
Cabrio3000
Walsh3100
Fleaur3100
Khoo3100
Davies3100
Stiles3200
Nayer3200
Taylor3200
McCain3200
Bissot3300
Mallin3300
Dellinger 3400
Rajs 3500
Dilly 3600
Ladwig3600
Chung3800
Everett 3900
Bell 4000
Bull 4100
Sarchand 4200
Lorentz 4200
Whalen4400
Pataballa 4800
Austin4800
Higgins 12008
Greenberg 12008
Hartstein 13000
Partners 13500
Russell 14000
De Haan 17000
Kochhar 17000
King 24000
57 rows selected.
6、优先级(可以使用括号改变优先级顺序)
优先级 | |
1 | 算数运算符 |
2 | 连接符 |
3 | 比较符 |
4 | IS [NOT] NULL, LIKE, [NOT] IN |
5 | [NOT] BETWEEN |
6 | 不等于 |
7 | NOT |
8 | AND |
9 | OR |
1、查找部门为SA_REP或者AD_PRES部门里面工资大于15000的员工名字、job_id、工资等信息
SELECT last_name, job_id, salary FROM employees WHERE job_id = 'SA_REP' OR job_id = 'AD_PRES' AND salary > 15000;
2、查找job_id 为SA_REP或者AD_PRES部门里面工资大于15000的员工姓名、job_id
SELECT last_name, job_id, salary FROM employees WHERE (job_id = 'SA_REP' OR job_id = 'AD_PRES') AND salary > 15000;
3、列出工资不在 5000-12000,部门在 20 或 50 的员工的姓名和工资
select last_name,department_id,salary from employees where salary not between 5000 and 12000 and department_id=20 or department_id=50;
或
select last_name,department_id,salary from employees where salary not between 5000 and 12000 and department_id in(20,50);