SQL查询语句

1. where 子句紧随 from 子句

2. 查询 last_name 为 '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'

正确

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'

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

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)

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 '\'

9. 查询 commission_pct 字段为空的所有员工信息
select last_name, commission_pct
from employees
where commission_pct is null

10. 查询 commission_pct 字段不为空的所有员工信息
select last_name, commission_pct
from employees
where commission_pct is not null

11. order by:
1). 若查询中有表达式运算, 一般使用别名排序
2). 按多个列排序: 先按第一列排序, 若第一列中有相同的, 再按第二列排序.
格式: order by 一级排序列 asc/desc,二级排序列 asc/desc

 

SQL> desc employee01
Name Type Nullable Default Comments
-------------- ------------ -------- ------- --------
EMPLOYEE_ID NUMBER(6) Y
FIRST_NAME VARCHAR2(20) Y
LAST_NAME VARCHAR2(25)
EMAIL VARCHAR2(25)
PHONE_NUMBER VARCHAR2(20) Y
HIRE_DATE DATE
JOB_ID VARCHAR2(10)
SALARY NUMBER(8,2) Y
COMMISSION_PCT NUMBER(2,2) Y
MANAGER_ID NUMBER(6) Y
DEPARTMENT_ID NUMBER(4) Y

posted @ 2016-10-14 19:51  煮酒听雨  阅读(212)  评论(0编辑  收藏  举报