伪列
rowid: 当前数据在数据库中的物理位置.
特点: 不存在任何表中,能查询出来.
-- 查看员工的工号,名字,薪资,及其物理位置id select employee_id,first_name,salary,rowid from employees;
rownum: 对满足查询条件的,查询结果进行编号(从1开始)
特点:
对满足where条件的查询结果进行标序号.
序号一定从1开始,且连续整数值.
rownum做条件判断: <n <=n >=1 =1
-- 查看员工的工号,名字,薪资, 每行数据添加编号(1-开始)
select employee_id,frist_name,salary,rownum
from employees;
-- 查看薪资大于10000的员工信息: 工号,名字,薪资,序号 rownum.
select employee_id,first_name,salary,rownum
from employees
where salary>10000;
-- 查看员工表中,前10个员工信息(工号,名字,薪资,序号)[序号<=10]
select employee_id,first_name,salary,rownum
from employees
where rownum <= 10;
-- 查看员工表中,第11~第20个员工信息(工号,名字,薪资,序号)
select employee_id,first_name,salary,rownum
from employees
where rownum >10 and rownum <=20; [×]
子查询
概念: 一个查询语句执行过程,需要用到另一条查询sql(子查询)的执行结果?
-- 具有x薪资的员工信息(工号,名字,薪资)? select * from employees where salary = X; --x 是最高薪资? select max(salary) from employees; -- x
1. 子查询结果是1行1列的值
将子查询的结果作为主查询的条件判断.
-- 具有最高薪资的员工信息(工号,名字,薪资)?
思路:
1. 获得员工的最高薪资 maxSalary
select max(salary) from employees;--maxSalary;
2. 获得薪资为maxSalary的员工信息(工号,名字,薪资)
select employee_id,first_name,salary from employees where salary = (maxSalary);
3. 合并SQL
select employee_id,first_name,salary from employees where salary = (select max(salary) from employees);
-- 查询薪资数大于平均工资员工信息(工号,名字,薪资)
1. 获得平均工资 avgSalary
select avg(salary) from employees;--avgSalary
2. 获得薪资>avgSalary的员工信息?
select * from employees where salary > (avgSalary);
3. 合并
select * from employees where salary > (select avg(salary) from employees);
2. 子查询的结果是n行1列的值
将子查询的结果作为主查询的条件判断,且一般使用多值 枚举判断 in
-- 查询和姓 'King' 的在同一部门的员工信息?
1. 获得king所在的部门id: --deptId
select department_id from employees where last_name ='King';--DeptId
2. 查询部门id为deptId的员工信息?
select * from employees where department_id in (DeptId)
3. 合并:
select * from employees where department_id in (select department_id from employees where last_name ='King');
3. 子查询的结果是n行n列
主查询将子查询的结果作为临时表,再次查询.
-- 查看员工信息(工号,名字,薪资),按照薪资降序排序的前10名员工信息?
select employee_id,first_name,salary
from employees
where rownum <=10
order by salary desc;[错误: 按理先排序后取前10个,实际sql执行顺序先获取前10后排序]
人工思路:
1. 先按照薪资降序排序---排序后的结果
select employee_id,first_name,salary from employees order by salary desc; -- EMP1 表-- n行n列的结果 临时表
2. 对排序后的结果取前10
select *
from (EMP1)
where rownum <=10;
3. 合并
select * from
(select employee_id,first_name,salary from employees order by salary desc)
where rownum <=10;
分页查询[重点]
-- 1. 查看员工表中,第11~第20个员工信息(工号,名字,薪资,序号)
思路:
1. 获得拥有序号效果的列的EMP1表?[对表添加一个rownum的RN的序号列]
select employee_id,first_name,salary,rownum as rn
from employees;--EMP1
2. 查询EMP1中RN的值为11~20之间的员工信息?[对步骤1的查询临时表,再次查询]
select employee_id,first_name,salary,rn
from (emp1)
where rn >=11 and rn <=20;
3. 合并
select employee_id,first_name,salary,rn
from
(select employee_id,first_name,salary,rownum as rn from employees)
where rn >=11 and rn <=20;
-- 2. 查询员工信息按照薪资降序排序后的第11条~第20条员工信息?[重要, 笔试, 100%开发]
1. 按照薪资降序排序:
select * from employees order by salary desc;-- t1
2. 对t1表 添加RN序号列
select employee_id,first_name,salary,rownum rn
from (t1);--t2
3. 获取RN 11~20之间的员工信息?
select employee_id,first_name,salary,rn
from (t2)
where rn >=11 and rn <=20;
4. 合并
select employee_id,first_name,salary,rn
from
(select employee_id,first_name,salary,rownum rn
from
(select * from employees order by salary desc))
where rn >=11 and rn<=20;