含义:出现在其他语句中的select语句,称为子查询或内查询;外部的查询语句,称为主查询或外查询
分类:
① 标量子查询(单行子查询)
② 列子查询(多行子查询)
③ 行子查询 (多行多列)
特点:
① 子查询放在小括号内
② 子查询一般放在条件的右侧
③ 标量子查询,一般搭配着单行操作符使用( >、 <、 >=、 <=、 =、 <>)
④ 列子查询,一般搭配着多行操作符使用(in、any/some、all、)
⑤ 子查询的执行优先于主查询执行,主查询的条件用到了子查询的结果
select salary from employees where last_name = "Abel";
② 查询员工的信息,满足 salary > ①结果
select * from employees where salary >(select salary from employees where last_name = "Abel");
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);
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);
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);
多行比较操作符
in / not in :等于列表中的任意一个
any | some :和子查询返回的某一个值比较
all : 和子查询返回的所有值比较
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));
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");
select last_name,employee_id,job_id,salary from employees where salary < all(select distinct salary from employees where job_id = "IT_PROG");
select * from employees where (employee_id,salary)=(select min(employee_id),max(salary) from employees);
注意:仅仅支持标量子查询
select d.*,(select count(*) from employees e where e.department_id = d."department_id") 个数 from departments d;
select (select department_name from departments d inner join employees e on d.department_id = e.department_id where e.employee_id = 102) 部门名;
将子查询结果充当一张表,要求必须起别名
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;
查看是否存在
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可以省略
select * from employees limit 10,15;
select * from employees where commission_pct is not null order by salary desc limit 10;
union : 联合 合并 将多条查询语句的结果合并成一个结果
语法:
查询语句1
union
查询语句2
union
....
应用场景:要查询的结果来自于多个表,且多个表没有直接的连接关系,但查询信息一致时
特点:
① 要求多条查询语句的查询列数是一致的
② 要求多条查询语句的查询的每一列的类型和顺序最好一致
③ union关键字默认去重,如果使用union all 可以包含重复项
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";