目录
前言
数据表结构
数据库文件(按照顺序导入)
先导入del_data.sql
再导入
hr_cre.sql
最后导入hr_popul.sql
提取码:69z3
1基本SQL-SELECT
1.1基本SQL-SELECT语句笔记
笔记
1. 对于日期型数据, 做 *, / 运算不合法
2. 包含空值的数学表达式的值都为空值
3. 别名使用双引号!
4. oracle 中连接字符串使用 "||", 而不是 java 中的 "+"
5. 日期和字符只能在单引号中出现. 输出 last_name`s email is email
select last_name || ' `s email is ' || email EMAIL
from employees
6. distinct 关键字, 以下语法错误
select last_name, distinct department_id
from employees
1.2 基本SQL-SELECT语句练习
练习
1. SQL*PLUS命令可以控制数据库吗? 否!
2. 下面的语句是否可以执行成功 可以
select last_name , job_id , salary as sal
from employees;
3. 下面的语句是否可以执行成功 可以
select * from employees;
4. 找出下面语句中的错误 标点符号需要是英文格式下的。
select employee_id , last_name,
salary * 12 “ANNUAL SALARY”
from employees;
5. 显示表departments的结构,并查询其中的全部数据
desc departments;
select * from departments;
6. 显示出表employees中的全部job_id(不能重复)
Select distinct job_id from employees;
7. 显示出表employees的全部列,各个列之间用逗号连接,列头显示成OUT_PUT
a) select employee_id ||','|| last_name||','||salary "OUT_PUT"
b) from employees
2过滤和排序数据
2.1过滤和排序数据笔记
笔记
7. WHERE 子句紧随 FROM 子句
8. 查询 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'
9. 查询 1998-4-24 来公司的员工有哪些?
注意: 日期必须要放在单引号中, 且必须是指定的格式
select last_name, hire_date
from employees
where hire_date = '24-4月-1998'
10. 查询工资在 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
11. 查询工资等于 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)
12. 查询 LAST_NAME 中有 'o' 字符的所有员工信息.
select *
from employees
where last_name like '%o%'
13. 查询 LAST_NAME 中第二个字符是 'o' 的所有员工信息.
select *
from employees
where last_name like '_o%'
14. 查询 LAST_NAME 中含有 '_' 字符的所有员工信息
1). 准备工作:
update employees
set last_name = 'Jones_Tom'
where employee_id = 195
2). 使用 escape 说明转义字符.
select *
from employees
where last_name like '%\_%' escape '\'
15. 查询 COMMISSION_PCT 字段为空的所有员工信息
select last_name, commission_pct
from employees
where commission_pct is null
16. 查询 COMMISSION_PCT 字段不为空的所有员工信息
select last_name, commission_pct
from employees
where commission_pct is not null
17. ORDER BY:
1). 若查询中有表达式运算, 一般使用别名排序
2). 按多个列排序: 先按第一列排序, 若第一列中有相同的, 再按第二列排序.
格式: ORDER BY 一级排序列 ASC/DESC,二级排序列 ASC/DESC;
2.2过滤和排序数据练习
测 试
1. 查询工资大于12000的员工姓名和工资
a) select last_name,salary
b) from employees
c) where salary > 12000
2. 查询员工号为176的员工的姓名和部门号
a) select last_name,department_id
b) from employees
c) where employee_id = 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
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'
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)
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'
7. 选择公司中没有管理者的员工姓名及job_id
a) select last_name,job_id
b) from employees
c) where manager_id is null
8. 选择公司中有奖金的员工姓名,工资和奖金级别
a) select last_name,salary,commission_pct
b) from employees
c) where commission_pct is not null
9. 选择员工姓名的第三个字母是a的员工姓名
a) select last_name
b) from employees
c) 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%'
3单行函数
3.1单行函数笔记
18. 打印出 "2009年10月14日 9:25:40" 格式的当前系统的日期和时间.
select to_char(sysdate, 'YYYY"年"MM"月"DD"日" HH:MI:SS')
from dual
注意: 使用双引号向日期中添加字符
19. 格式化数字: 1234567.89 为 1,234,567.89
select to_char(1234567.89, '999,999,999.99')
from dual
20. 字符串转为数字时
1). 若字符串中没有特殊字符, 可以进行隐式转换:
select '1234567.89' + 100
from dual
2). 若字符串中有特殊字符, 例如 '1,234,567.89', 则无法进行隐式转换, 需要使用 to_number() 来完成
select to_number('1,234,567.89', '999,999,999.99') + 100
from dual
21. 对于把日期作为查询条件的查询, 一般都使用 to_date() 把一个字符串转为日期,
这样可以不必关注日期格式
select last_name, hire_date
from employees
where hire_date = to_date('1998-5-23', 'yyyy-mm-dd')
-- where to_char(hire_date,'yyyy-mm-dd') = '1998-5-23'
22. 转换函数: to_char(), to_number(), to_date()
23. 查询每个月倒数第 2 天入职的员工的信息.
select last_name, hire_date
from employees
where hire_date = last_day(hire_date) - 1
24. 计算公司员工的年薪
--错误写法: 因为空值计算的结果还是空值
select last_name, salary * 12 * (1 + commission_pct) year_sal
from employees
--正确写法
select last_name, salary * 12 * (1 + nvl(commission_pct, 0)) year_sal
from employees
25. 查询部门号为 10, 20, 30 的员工信息, 若部门号为 10, 则打印其工资的 1.1 倍,
20 号部门, 则打印其工资的 1.2 倍, 30 号部门打印其工资的 1.3 倍数
--使用 case-when-then-else-end
select last_name, department_id, salary, case department_id when 10 then salary * 1.1
when 20 then salary * 1.2
when 30 then salary * 1.3
end new_sal
from employees
where department_id in (10, 20, 30)
--使用 decode
select last_name, department_id, salary, decode(department_id, 10, salary * 1.1,
20, salary * 1.2,
30, salary * 1.3
) new_sal
from employees
where department_id in (10, 20, 30)
3.2单行函数练习
测 试
1. 显示系统时间(注:日期+时间)
a) select to_char(sysdate,'yyyy-mm-dd hh:mi:ss')
b) from dual
2. 查询员工号,姓名,工资,以及工资提高百分之20%后的结果(new salary)
a) select employee_id,last_name,salary,salary*1.2 "new salary"
b) from employees
3. 将员工的姓名按首字母排序,并写出姓名的长度(length)
a) select last_name,length(last_name)
b) from employees
c) order by last_name asc
4. 查询各员工的姓名,并显示出各员工在公司工作的月份数(worked_month)。
a) select last_name,hire_date,round(months_between(sysdate,hire_date),1) workded_month
b) from employees
5. 查询员工的姓名,以及在公