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