数据库基础篇学习笔记_子查询

子查询

1. 子查询的使用与分类

1.1 子查询的基本使用

子查询指一个查询语句嵌套在另一个查询语句内部的查询,这个特性从MySQL 4.1开始引入

SQL 中子查询的使用大大增强了 SELECT 查询的能力,因为很多时候查询需要从结果集中获取数据或者需要从同一个表中先计算得出一个数据结果,然后与这个数据结果(可能是某个标量,也可能是某个集合)进行比较

  • 子查询的基本语法结构

    SELECT select_list
    FROM table
    WHERE expr operator
    				(SELECT select_list
                     FROM table);
    
    • 子查询(内查询)在主查询之前一次执行完成
    • 子查询的结果被主查询(外查询)使用
    • 注意事项:
      1. 子查询要包含在括号内
      2. 将子查询放在比较条件的右侧
      3. 单行操作符对应单行子查询,多行操作符对应多行子查询

1.2 子查询的分类

  • 分类方式1:

    按内查询的结果返回一条还是多条记录,将子查询分为:单行子查询和多行子查询

  • 分类方式2:

    按内查询是否被执行多次,将子查询划分为 相关(或关联)子查询和不相关(或非关联)子查询

2. 单行子查询

2.1 单行比较操作符

操作符                  含义
=                      equal to 
>                      greater than 
>=                     greater than or equal to 
<                      less than 
<=                     less than or equal to 
<>                     not equal to

2.2 代码示例

题目:查询工资大于149号员工工资的员工的信息

select last_name salary
from employees
where salary > ( select salary
                 from employees
                 where employee_id = 149);

题目:返回job_id与141号员工相同,salary比143号员工多的员工姓名,job_id和工资

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

select last_name,job_id,salary
from employees
where salary = (select min(salary)
               	from employees);

题目:查询与141号manager_id和department_id相同的其他员工的employee_id,manager_id,department_id

select employee_id,manager_id,department_id
from employees
where manager_id = (select manager_id
                   from employees
                   where employee_id = 141)
and department_id = (select department_id
                    from employees
                    where employee_id = 141)
and employee_id <> 141;             

2.3 HAVING中的子查询

  • 首先执行子查询
  • 向主查询中的HAVING子句返回结果

题目:查询最低工资大于50号部门最低工资的部门id和其最低工资

select department_id,min(salary)
from employees
group by department_id
having min(salary) > (select min(salary)
                     from employees
                     where department_id = 50);

2.4 Case中的子查询

题目:显示员工的employee_id,last_name和location.其中,若员工department_id与location_id为1800的department_id相同,则location为’Canada’,其余则为’USA’

SELECT employee_id, last_name, 
		(CASE department_id 
         WHEN 
         (SELECT department_id 
          FROM departments WHERE location_id = 1800) 
         THEN 'Canada' ELSE 'USA' END) location 
FROM employees;

2.5 单行子查询的空值情况

子查询不返回任何行

3. 多行子查询

3.1 多行比较操作符

操作符                  含义
IN                     等于列表中的任意一个
ANY                    需要和单行比较操作符一起使用,和子查询返回的某一个值比较
ALL                    需要和单行比较操作符一起使用,和子查询返回的所有值比较
SOME                   实际上是ANY的别名,作用相同,一般常使用ANY

3.2 代码示例

题目:返回其它job_id中比job_id为‘IT_PROG’部门任一工资低的员工的员工号、姓名、job_id以及salary

select employee_id,last_name,job_id,salary
from employees
where job_id <> 'IT_PROG'
and salary < any(select salary
				from employees
				where job_id = 'IT_PROG');

题目:返回其它job_id中比job_id为‘IT_PROG’部门所有工资低的员工的员工号、姓名、job_id以及salary

select employee_id,last_name,job_id,salary
from employees
where job_id <> 'IT_PROG'
and salary < all(select salary
				from employees
				where job_id = 'IT_PROG');

题目:查询平均工资最低的部门id

#方式1:
select department_id
from employees
group by department_id
having avg(salary) = (select min(avg_salary)
					from(select avg(salary) avg_salary
					from employees
					group by department_id) tab_avg_salary);

#方式2:
select department_id
from employees
group by department_id
having avg(salary) <= all(select avg(salary)
					from employees
					group by department_id);


3.3 多行子查询空值情况

存在空值,无返回结果

4. 相关子查询

4.1 相关子查询的执行流程

如果子查询的执行依赖于外部查询,通常情况下都是因为子查询中的表用到了外部的表,并进行了条件关联,因此每执行一次外部查询,子查询都要重新计算一次,这样的子查询就称之为(关联)子查询

相关子查询按照一行接一行的顺序执行,主查询的每一行都执行一次子查询

请添加图片描述

相关子查询的语法格式:

请添加图片描述

子查询中使用主查询中的列

4.2 代码示例

题目:查询员工中工资大于本部门平均工资的员工的last_name,salary和其department_id

select last_name,salary,department_id
from employees t1
where salary > (
				select avg(salary)
				from employees t2
    			where t2.department_id = t1.department_id
				);

题目:查询员工的id,salary,按照department_name排序

select employee_id,salary
from employees t1
order by (
		select department_name
		from departments t2
		where t1.department_id = t2.department_id);

在select结构中,除group by 和limit 子句,其他子句都可以使用子查询

题目:若employees表中employee_id与job_history表中employee_id相同的数目不小于2,输出这些相同id的员工的employee_id,last_name和其job_id

select employee_id,last_name,job_id
from employees t1
where 2 <= (
			select count(*)
			from job_history t2
			where t2.employee_id = t1.employee_id);

4.3 EXISTS与NOT EXISTS关键字

  • 关联子查询通常也会和 EXISTS操作符一起来使用,用来检查在子查询中是否存在满足条件的行
  • 如果在子查询中不存在满足条件的行
    • 条件返回 FALSE
    • 继续在子查询中查找
  • 如果在子查询中存在满足条件的行
    • 条件返回 TRUE
    • 不在子查询中继续查找
  • NOT EXISTS关键字表示如果不存在某种条件,则返回TRUE,否则返回FALSE

题目:查询公司管理者的employee_id,last_name,job_id,department_id信息

select employee_id,last_name,job_id,department_id
from employees t1
where exists(
			select *
			from employees t2
			where t1.employee_id = t2.manager_id);

题目:查询departments表中,不存在于employees表中的部门的department_id和department_name

select department_id,department_name
from departments t1
where not exists (select *
                 from employees t2
                 where t1.department_id = t2.department_id);

4.4 相关更新

使用相关子查询依据一个表中的数据更新另一个表的数据

UPDATE table1 alias1 
SET column = (SELECT expression 
              FROM table2 alias2 
              WHERE alias1.column = alias2.column);

4.5 相关删除

使用相关子查询依据一个表中的数据删除另一个表的数据

DELETE FROM table1 alias1 
WHERE column operator (
    SELECT expression 
    FROM table2 alias2 
    WHERE alias1.column = alias2.column);
ion 
              FROM table2 alias2 
              WHERE alias1.column = alias2.column);

4.5 相关删除

使用相关子查询依据一个表中的数据删除另一个表的数据

DELETE FROM table1 alias1 
WHERE column operator (
    SELECT expression 
    FROM table2 alias2 
    WHERE alias1.column = alias2.column);

猜你喜欢

转载自blog.csdn.net/weixin_51636172/article/details/125966944