一、子查询
含义:出现在其他语句中的select语句,称为子查询或内查询;外部的查询语句,称为主查询或外查询
分类:
- 按子查询出现的位置:
① select 后面 :仅仅支持标量子查询
② from 后面 :支持表子查询
③ where 或 having后面 :支持标量子查询(☆),列子查询(☆),行子查询(用的比较少)
④ exists后面(相关子查询):支持表子查询 - 按结果集的行列数不同:
① 标量子查询(结果集只有一行一列)
② 列子查询(结果集只有一列多行)
③ 行子查询(结果集是一行多列)
④ 表子查询(结果集一般为多行多列)
1. where或having后面
① 标量子查询(单行子查询)
② 列子查询(多行子查询)
③ 行子查询 (多行多列)
特点:
① 子查询放在小括号内
② 子查询一般放在条件的右侧
③ 标量子查询,一般搭配着单行操作符使用( >、 <、 >=、 <=、 =、 <>)
④ 列子查询,一般搭配着多行操作符使用(in、any/some、all、)
⑤ 子查询的执行优先于主查询执行,主查询的条件用到了子查询的结果
1.1 标量子查询
- 案例
① 查询Abel的工资
select salary from employees where last_name = "Abel";
② 查询员工的信息,满足 salary > ①结果
select * from employees where salary >(select salary from employees where last_name = "Abel");
- 返回job_id 与141号员工相同,salary 比143号员工多的员工姓名,job_id和工资(where)
① 查询141号员工的job_id
select job_id from employees where employee_id = 141;
② 查询143号员工的salary
select salary from employees where employee_id = 143;
③ 查询员工的姓名,job_id 和工资,要求 job_id = ① 并且 salary > ②
select last_name,job_id,salary from employees where job_id = (select job_id from employees where employee_id = 141) and salary > (select salary from employees where employee_id = 143);
- 返回公司工资最少的员工的last_name,job_id 和salary(where)
① 查询公司最低工资
select min(salary) from employees;
② 查询last_name, job_id 和 salary ,要求 salary = ①
select last_name,job_id,salary from employees where salary = (select min(salary) from employees);
- 查询最低工资大于(50号部门最低工资)的部门id和其最低工资
① 查询50号部门的最低工资
select min(salary) from emploees where department_id = 50;
② 查询每个部门的最低工资
select min(salary). department_id from employees group by department_id;
③ 在②的基础上筛选,满足min(salary) > ①
select min(salary). department_id from employees group by department_id having min(salary) > (select min(salary) from emploees where department_id = 50);
1.2 列子查询(多行子查询)
多行比较操作符
in / not in :等于列表中的任意一个
any | some :和子查询返回的某一个值比较
all : 和子查询返回的所有值比较
- 返回location_id 是1400 或是 1700 的部门中的所有员工姓名
① 查询location_id 是1400 或是 1700 的部门编号
select distinct department_id from departments where location_id in(1400,1700);
② 查询员工姓名,要求部门号是①列表中的某一个
select last_name from employees where department_id in(select distinct department_id from departments where location_id in(1400,1700));
- 返回其他部门中比job_id为“IT_PROG”部门任一(any)工资低的员工的员工号,姓名,job_id 以及 salary
① 查询job_id 为 “IT_PROG”部门任一工资
select distinct salary from employees where job_id = "IT_PROG";
② 员工的员工号,姓名,job_id 以及 salary,salary < (①)中的任意一个
select last_name,employee_id,job_id,salary from employees where salary < any(select distinct salary from employees where job_id = "IT_PROG");
- 返回其他部门中比job_id为“IT_PROG”部门所有(all)工资低的员工的员工号,姓名,job_id 以及 salary
select last_name,employee_id,job_id,salary from employees where salary < all(select distinct salary from employees where job_id = "IT_PROG");
1.3 行子查询(结果集一行多列或多行多列)
- 查询员工编号最小并且工资最高的员工信息
select * from employees where (employee_id,salary)=(select min(employee_id),max(salary) from employees);
2. select后面
注意:仅仅支持标量子查询
- 查询每个部门的员工个数
select d.*,(select count(*) from employees e where e.department_id = d."department_id") 个数 from departments d;
- 查询员工号 = 102 的部门名
select (select department_name from departments d inner join employees e on d.department_id = e.department_id where e.employee_id = 102) 部门名;
3. from后面
将子查询结果充当一张表,要求必须起别名
- 查询每个部门的平均工资的工资等级
① 查询每个部门的平均工资
select avg(salary),department_id from employees group by department_id;
② 连接①的结果集和job_grades 表,筛选条件平均工资 between lowest_sal and highest_sal
select from (select avg(salary),department_id from employees group by department_id)
ag_dep inner join job_grades g on ag_dep inner join job_grades g on ag_dep.ag between lowest_sal and highest_sal;
4. exists后面(相关子查询)
查看是否存在
- 查询有员工的部门名
select department_name from departments d where exists(select * from employees e where d."department_id" = e."department_id");
- 查询没有女盆友的男神信息
//in
select bo.*from boys bo where bo.id not in (select boyfriend_id from beauty);
//exists
select bo.* from boys bo where not exists(select boyfriend_id from beauty b where bo."id" = b."boyfriend_id");
二、分页查询
应用场景:当要显示的数据,一页显示不全,需要分页提交sql请求
语法:
select 查询列表
from 表
【join type】 join 表2
on 连接条件
where 筛选条件
group by 分组字段
having 分组后的筛选
order by 排序后的字段
limt offset,size;
//offset 要显示条目的起始索引(起始索引从0 开始)
//size 要显示的条目个数
特点:
① limit语句放在查询语句的最后
② 公式
要显示的页数 page , 每页的条目数 size
select 查询列表 from 表名 limit (page - 1) * size,size;
- 查询前五条员工信息
select * from employees limit 0,5;
select * from employees limit 5; //从起始位置开始查询时,0可以省略
- 查询第11条 到15 条
select * from employees limit 10,15;
- 查询有奖金的员工信息,并且工资较高的钱前10名显示出来
select * from employees where commission_pct is not null order by salary desc limit 10;
三、联合查询
union : 联合 合并 将多条查询语句的结果合并成一个结果
语法:
查询语句1
union
查询语句2
union
....
应用场景:要查询的结果来自于多个表,且多个表没有直接的连接关系,但查询信息一致时
特点:
① 要求多条查询语句的查询列数是一致的
② 要求多条查询语句的查询的每一列的类型和顺序最好一致
③ union关键字默认去重,如果使用union all 可以包含重复项
- 查询部门编号 > 90 或邮箱包含 a 的员工信息
select * from employees where email like "%a%" or department_id > 90;
select * from employees where email like "%a%"
union
select * from employees where department_id > 90;
- 查询中国用户中男性的信息以及外国用户中年男性的用户信息
select id,cname,csex from t_ca where csex = "男"
union
select t_id,tName,tGender from t_ua where tGender= "male";