1. WHERE 子句紧随 FROM 子句
这个是需要大家注意的,他们两必须挨着,order by不能贴在这两之间
2. 查询 last_name 为 'King' 的员工信息
这个我们看一下就可以了
由于King它是一个字符串,所以我们得给他加上一对单引号
错误1: King 没有加上 单引号
select first_name, last_name
from employees
where last_name = King
错误2: 在单引号中的值区分大小写
select first_name, last_name
from employees
where last_name = 'king'
第一个错误没加,所以错了,加了单引号的字符串是严格区分大小写的,前面都是SQL的,它是不区分的,
没有问题,尤其像select,from,where,作为三个关键字,大写,小写,都行,但是作为字符串的,你一定要区分大小写,
所以这种查询是没有结果的,必须得这样才行,这个开始我们讲的时候也说到了这一点,你再回顾一下
正确
select first_name, last_name
from employees
where last_name = 'King'
3. 查询 1998-4-24 来公司的员工有哪些?
注意: 日期必须要放在单引号中, 且必须是指定的格式
select last_name, hire_date
from employees
where hire_date = '24-4月-1998'
我们那会也做了一个类似的,这个格式我们说要非常的严格,如果你稍有不慎,写错了,就不行,
我们当时也给大家说了,你也可以怎么写,也可以这样写,我们下一节单行函数给大家讲到,我先写到这,我们使用to_char()
转换函数将date转化成一个字符串,yyyy表示年,mm表示月,dd表示day
where to_char(hire_date,'yyyy-mm-dd') = '1998-04-24,让他等于1998年,4月你可以写成04,这一天来公司的,
这两种写法都可以,不建议使用where hire_date = '24-4月-1998'这种写法,以后我们都抛弃他了,
4. 查询工资在 5000 -- 10000 之间的员工信息.
1). 使用 AND
select *
from employees
where salary >= 5000 and salary <= 10000
2). 使用 BETWEEN .. AND .., 注意: 包含边界!!
select *
from employees
where salary between 5000 and 10000
我查询他的last_name和salary,就两个信息,from employees,where加一个过滤条件,工资在5000和10000之间的,
就是salary大于等于5000,and salary小于等于1万
select last_name,salary from employees where salary>=5000 and salary<=10000
他们之间满足的,或者怎么写,我们还讲了between and,between 5000 and 10000,
select last_name,salary from employees where salary between 5000 and 10000
它是包含边界的,也是43条记录,如果你的是严格大于5000,也小于1万的,那你用between and就不合适了,
只能够用这种方式,因为它是含边界的
5. 查询工资等于 6000, 7000, 8000, 9000, 10000 的员工信息
1). 使用 OR
select *
from employees
where salary = 6000 or salary = 7000 or salary = 8000 or salary = 9000 or salary = 10000
2). 使用 IN
select *
from employees
where salary in (6000, 7000, 8000, 9000, 10000)
这个我们不写了,你们看一下就行,这种方法,你或者使用or,将salary一个个写出来,用or连接,我们看一下这个结果,
17条记录,或者我们使用in,表示salary取中间每一个之后,结果是多少,这两种方式都可以
6. 查询 LAST_NAME 中有 'o' 字符的所有员工信息.
select *
from employees
where last_name like '%o%'
前边有不确定多个,后边有不确定多个
7. 查询 LAST_NAME 中第二个字符是 'o' 的所有员工信息.
select *
from employees
where last_name like '_o%'
你只要把前面改成下划线就可以了
8. 查询 LAST_NAME 中含有 '_' 字符的所有员工信息
1). 准备工作:
update employees
set last_name = 'Jones_Tom'
where employee_id = 195
2). 使用 escape 说明转义字符.
select *
from employees
where last_name like '%\_%' escape '\'
涉及到我们讲的转义字符了,那你就得这样写
like,加上一个有转义字符的标识,表明它是一个转义字符,就是表示名字中含有下划线
已经不再表示是一个单个字符的含义了,
9. 查询 COMMISSION_PCT 字段为空的所有员工信息
select last_name, commission_pct
from employees
where commission_pct is null
为空就是is null呗
10. 查询 COMMISSION_PCT 字段不为空的所有员工信息
select last_name, commission_pct
from employees
where commission_pct is not null
不为空就是is not null
这个比较简单,所以这一节内容难度不大
11. ORDER BY:
1). 若查询中有表达式运算, 一般使用别名排序
2). 按多个列排序: 先按第一列排序, 若第一列中有相同的, 再按第二列排序.
格式: ORDER BY 一级排序列 ASC/DESC,二级排序列 ASC/DESC;
order by排序,可以按照多个列来排,先按照第一列,如果第一列有相同的,再按第二列,这两列你都可以
按照顺序和逆序的顺序排,都行,如果你再右需要的话,按照第三列,可以依次这样下去,这是我们这一节的基本的
小练习
1. 查询工资大于12000的员工姓名和工资
a) select last_name,salary
b) from employees
c) where salary > 12000
没有等号的就不用加了,大于12000
2. 查询员工号为176的员工的姓名和部门号
a) select last_name,department_id
b) from employees
c) where employee_id = 176
工号为176的姓名和部门号
3. 选择工资不在5000到12000的员工的姓名和工资
a) select last_name,salary
b) from employees
c) --where salary < 5000 or salary > 12000
d) where salary not between 5000 and 12000
选择工资不在5000到12000的,在怎么写,last_name,salary,from employees,
where,不在5000到12000的,就是这salary,我们换一种表述方式,就是salary小于5000,
应该是or而不是and,或者大于1万2,是这个意思吧
55个,还能怎么写,我们那会是不是讲了一个between and,如果是5000到12000的怎么表示,salary between 5000
and 12000,如果说不在呢,那是不是就叫not between
我们看这个结果是多少,刚才是55,这是不是也是55,所以说这两种写法都可以,
4. 选择雇用时间在1998-02-01到1998-05-01之间的员工姓名,job_id和雇用时间
a) select last_name,job_id,hire_date
b) from employees
c) --where hire_date between '1-2月-1998' and '1-5月-1998'
d) where to_char(hire_date,'yyyy-mm-dd') between '1998-02-01' and '1998-05-01'
选择雇佣时间在这个时间之内的,select last_name,job_id,hire_date from employees where
我们先按照旧的写法,看看怎么写,98年2月1日,where hire_date between,一对单引号,2月1日,
就是1-2月-1998年的,and,'1-5月-1998',这两个日期之间的
那就11个人,这种写法我们也不建议写,我们来写一下吧,hire_date,between,这样写,这个操作还不太一样,
因为你这儿写的是hire_date,也能写,这样,我们后面会给大家说,你既可以把一个date型的转成一个char,
也可以把一个char转成date型,这两种方式都可以,我们这是把date型转成char,现在这是一个char型,字符串
类型的,他between 1988-02-01,and,1998-05-01,
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'
11条,和我们刚才的记录是一样的,这也是两种方式,也是两种方式,大家都看一下,你掌握哪一种,掌握后面这一种,
我们在下一节还会讲,这是前4个题目
5. 选择在20或50号部门工作的员工姓名和部门号
a) select last_name,department_id
b) from employees
c) where department_id = 20 or department_id = 50
d) --where department_id in (20,50)
选在在20号或50号部门的员工,last_name,department_id,from employees,where,那就是department_id,20或50,
那可以写成这样,department_id=20 or department_id=50,可以这样写吧,还可以怎么写,where department_id in,
20,50,两种方式都可以
6. 选择在1994年雇用的员工的姓名和雇用时间
a) select last_name,hire_date
b) from employees
c) --where hire_date like '%94'
d) where to_char(hire_date,'yyyy') = '1994'
在94奶奶雇佣员工的姓名和时间,94年来的,就是这个意思呗,94来公司的员工,select last_name,hire_date
from employees where,如何来表示94来公司的,肯定还是对hire_date进行操作,说他94年来的,正常hire_date
是有年有日的,那你这儿是不是相当于一个模糊查询,模糊查询,你最后是以年,含有1994,前面有年有月有日,
select last_name,hire_date from employees where hire_date like '%1994'
没有,你这样写不行,我们把它去了试试
select last_name,hire_date from employees where hire_date like '%94'
这就有了
所以我们要写的话就这样写,或者我们再给大家说一种写法,涉及到刚才说的转换函数,where to_char(hire_date,'yyyy-mm-dd')
当你把它转成一个字符的时候,我就不要你这个月跟日了,只要你这个年,年字符就等于1994
select last_name,hire_date from employees where to_char(hire_date,'yyyy') = '1994'
这个我们看一下结果,刚才有7条记录
所以这样的写法,哪一种好,显然是下一种好,他更具有通用性,大家你写的时候,其实SQL语句,和我们开始学JAVA,
觉得是不难,反正挺绕的,因为你刚开始吧,接触这种语法格式的,你熟悉一下就行,那你select都是一些语句吧,
他都是一些语句,他还不能严格是一门编程语言,你要说PL/SQL还可以算,他就是一些语句,他实际上不难,你一定要
把这些练习敲一敲,要不觉得简单,学的快,忘得也快
7. 选择公司中没有管理者的员工姓名及job_id
a) select last_name,job_id
b) from employees
c) where manager_id is null
你做些练习的时候,有一个前提,你得把这个表里有哪些东西得知道,比如employees表,我们一直对他进行操作,
他有哪些列,是这个意思吧,他有哪些列你得清楚,你可以通过desc employees来看
就这些列,我们操作的其实都是这些列,department_id,salary,commission_pct,老板的id,等等等等都在这儿,
或者给大家一张表,接着说下面的练习,选择公司中没有管理者的员工姓名和job_id,select last_name,job_id
from employees,没有管理者,也就是where,你对应的manager_id is null
select last_name,job_id from employees where manager_is is null
这就是没有管理者
这个人它是没有管理者的,他就是公司的老板,名字就叫King
8. 选择公司中有奖金的员工姓名,工资和奖金级别
a) select last_name,salary,commission_pct
b) from employees
c) where commission_pct is not null
选择公司中有奖金的员工姓名,当你这个题目做多的时候,你脑子里看到这个东西,答案实际上已经出来了,
代码怎么写,脑子可能还没有想出来,还没有具体反应过来,手已经上去了,那你就到一定境界了,from employees,
有奖金,就意味着commission_pct不等于null
select last_name,salary,commission_pct from employees where commission_pct is not null
9. 选择员工姓名的第三个字母是a的员工姓名
a) select last_name
b) from employees
c) where last_name like '__a%'
员工姓名第三个字母是a,那就是前面有两个下划线,select last_name from employees where last_name,
第三个字符,得用like,模糊查询,加上一个单引号,两个下划线,第三个是a
select last_name from employees where last_name like '__a%'
后面可能还有不确定个字符,那就这样写
select last_name from employees where last_name like '__a%'
10. 选择姓名中有字母a和e的员工姓名
a) select last_name
b) from employees
c) where last_name like '%a%e%' or last_name like '%e%a%'
选择姓名中有字母a和e的员工,就是这个名字只要含有a或者e,都得给他输出,这个怎么写,
last_name我就输出他,from employees,"%a%e%"这样就含有a和e,但是这样写一个前提就是,
a在前,b在后,所以你再加上or,last_name like,把a和e的顺序颠倒一下
select last_name from employees where last_name like '%a%e%' or last_name like '%e%a%'
17个,你看这个名字的时候,所以是这样来操作,这个练习你务必看完以后,你一定要亲自敲一敲,
一定要亲自敲一敲,我们把这个练习保存一下,我们这一节过滤和排序数据就讲完了,下一节我们来讲单行函数