• 15
  • 14
分享
  • SQL教程子查询——软件测试圈
  • 饭团🍙 2021-02-03 10:48:27 字数 3952 阅读 1989 收藏 14

子查询

含义:

出现在其它语句中的select语句,称为子查询、内查询、嵌套查询

外部的查询语句,称为为主查询或外查询 

分类:

案子查询出现的位置:

  • select后面      仅支持标量子查询

  • from后面       支持表子查询

  • ※where或having后面    标量子查询※、列子查询※、行子查询(较少)

  • exists后面(相关子查询)

按结果集的行列数不同:

  • 标量子查询(结果集只有一行一列)

  • 行子查询(结果集只有一行多列)

  • 列子查询(结果集只有一列多行)

  • 表子查询(结果集一般为多行多列) 

一、where或having后面

  1. 标量子查询(单行子查询)

  2. 列子查询(多行子查询)

  3. 行子查询(单行多列) 

特点:

  1. 子查询放在小括号内

  2. 子查询一般放在条件的右侧

  3. 标量子查询,一般搭配着单行操作符使用> < >= <= = <>

  4. 子查询的执行优先于主查询执行,主查询的条件使用到了子查询的结果 

列子查询,一般搭配着多行操作符的使用

in、any/some、all 

1、标量子查询

#案例1:谁的工资比Abel高? 
select last_name
from employees 
where salary > ( 
    select salary 
     from employees  
    where last_name = 'Abel' 
);
 
#案例2:返回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 
);
   
#案例3:返回公司工资最少的员工的last_name, job_id和salary 
select last_name, job_id, salary 
from employees 
where salary = ( 
    select min(salary) 
    from employees 
);
  
#案例4:查询最低工资大于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、列子查询

11.png

#案例1:返回location_id是1400或1700的部门中所有的员工姓名
#使用表连接
select location_id, last_name
from employees e 
join departments d 
on e.department_id = d.department_id 
where location_id in (1400, 1700); 
#使用子查询(in关键字) 
select last_name
from employees 
where department_id in ( 
    select distinct department_id 
    from departments 
    where location_id in (1400, 1700) 
);  
 
#案例2:返回其它工种的,比job_id为‘IT_PROG’工种任一员工工资低的员工的:工号、姓名、job_id以及salary(any关键字)
select employee_id, last_name, job_id, salary 
from employees 
where department_id != 'IT_PROG'
and salary < any (
    select distinct salary 
    from employees 
    where job_id = 'IT_PROG' 
);
   
#案例3:返回其它工种的,比job_id为‘IT_PROG’工种所有员工工资都低的员工的:工号、姓名、job_id以及salary(all关键字)
select employee_id, last_name, job_id, salary
from employees 
where department_id != 'IT_PROG' 
and salary < all ( 
    select distinct salary 
    from employees 
    where job_id = 'IT_PROG'
); 
 
#案例4:问题同案例1,但是要用any或all            in 和 =any 等效 !!!       not in 和 != null等效 
select last_name 
from employees 
where department_id  = any ( 
    select distinct department_id
    from departments
    where location_id in (1400, 1700) 
);

3、行子查询 

#案例:查询员工编号最小,并且工资最高的员工的信息  
select * 
from employees 
where (employee_id, salary) = (
    select min(employee_id), max(salary) 
    from employees 
);

二、select后面

#案例1:查询每个部门的员工个数 
select d.*, (
    select count(*) 
    from employees e 
    where e.department_id = d.department_id 
) 个数 
from departments d; 
 
#案例2:查询员工号=102的部门名 
select (
    select department_name
    from departments d
    inner join employees e 
    on d.department_id = e.department_id 
    where e.employee_id = 102 
) 部门名;

三、from后面

#案例1:查询每个部门的平均工资的工资等级 
select ag_dep.*, g.grade_level
from (
    select department_id, avg(salary) ag
    from employees 
    group by department_id 
) ag_dep 
inner join job_grades g 
on ag_dep.ag between lowest_sal and highest_sal;

四、exists后面(相关子查询)

语法:

exists(完整的查询语句)

结果:

1或0

select exists(select employee_id from employees);

#案例1:查询有员工的部门名 
select department_name 
from departments d
where exists(
    select *
    from employees e
    where d.department_id = e.department_id 
);
   
#案例2:查询没有女朋友的男神信息 
#in 
select bo.* 
from boys bo 
where bo.id not in( 
    select boyfriend_id 
    from beauty b
)  
 
select bo.* 
from boys bo 
where not exists(
    select * 
    from beauty b
    where b.boyfriend_id = bo.id
);
 
#Test1:查询各部门中工资比本部门平均工资高的员工的员工号,姓名和工资
 select employee_id, last_name, salary,e.department_id
 from (
     select avg(salary) ag, department_id 
    from employees 
    group by department_id 
) ag_dep 
inner join employees e 
on e.department_id = ag_dep.department_id 
where salary > ag;     
 
#Test2:查询工资最高德员工的姓名,要求first_name和last_name显示为一列,列名为 姓.名
 select max(salary), concat(first_name, '.', last_name)
 from employees;

 

作者:请保持优秀。

原文链接:https://blog.csdn.net/IT_TIfarmer/article/details/110389287


  • 【留下美好印记】
    赞赏支持
登录 后发表评论
+ 关注

热门文章

    最新讲堂

      • 推荐阅读
      • 换一换
          •   找工作是出卖服务的过程。我们得先知道企业的需求是什么,才知道要怎么把自己卖出去,所以在面试之前,我们需要先分析企业的招聘需求。  企业的招聘要求里面会列出很多项内容。比如某公司的行政助理岗位,要求行政管理相关专业毕业、熟练使用Office、性格开朗、沟通能力强、积极主动等等。这些要求其实就是基于冰山模型的一套系统。  冰山模型简单为大家介绍一下。它是美国著名心理学家麦克利兰提出来的。用来表示个体素质的各个要素,现在成为很多企业进行人才筛选和培养的基础模型。  在该模型里面,知识与技能是冰山上面漏出海面的部分,它比较容易观测,而且相对而言容易后天习得,比如画PPT、做Excel等等;能力半浮...
            0 0 478
            分享
          • 测试工作中有时候会对数据库进行压力测试,jmeter实现这个需求较为简单,在这里简单介绍下。可以参考我之前写的15. Jmeter-配置元件二步骤:1、选中测试计划,添加mysql-connector-java-5.1.7-bin.jar插件2、添加一个线程组,设置线程组为103、选中线程组,添加一个配置元件:JDBC Connection Configuration4、对配置元件进行配置5、添加JDBC request6、对JDBC request 进行配置7、添加查看结果树8、运行测试计划作者:软件测试汪链接:https://www.jianshu.com/p/be496e4e00be
            0 0 1074
            分享
          • 一、接口自动化测试流程1、需求分析;请求(url、方法、数据)响应(响应数据、状态码)2、挑选需要做自动化接口(时间、人员、接口复杂度);3、设计自动化测试用例(如果功能阶段设计过用例,直接拿过来使用即可);4、搭建自动化测试环境(实现自动化使用的语言 如:(python、pycharm));5、设计自动化执行框架(报告、参数化、用例执行框架);6、编写代码;7、执行用例(unittest、pytest);8、生成测试报告(htmltextrunn er\allure)。二、接口清单整理登录接口请求登陆接口请求请求url:http://ttapi.research.itcast.cn/app/...
            0 0 1077
            分享
          • 一、等待的作用等待的作用在实际自动化实现过程中,都会添加等待来完善自动化测试的代码。自动化测试,是交由机器来执行的一种测试手段,用于提升测试效率,意味着每一次的自动化测试都 需要有非常高的成功率,才可以达到提升效率的作用。在自动化测试中,其实就是通过代码,来执行测试的流程,也就意味着机器知己对页面元素来进行操 作,如果说因为页面加载速度过慢,导致元素无法被第一时间找到,则报错,停止本次自动化测试, 通过添加等待的方法,让代码在运行时,会进行等待页面加载的操作,以便于更好的进行元素查找。我们平常用到的有三种等待方式:强制等待隐式等待显示等待二、三种等待方式1:强制等待第一种也是使用最简单的一种办...
            0 0 1347
            分享
          • 获取到一个上传项目信息的接口1)需求请求签名2)调用同一接口间隔时间不能小于1秒测试步骤1)模拟实际用户,使用阶梯型线程加压进行模拟,这里使用的是阶梯式加压 jp@gc-Stepping Thread Group (deprecated)2)添加用户参数组件3)添加JSR223取样器获取接口需要的请求签名信息JSR223取样器允许执行JSR223脚本代码用于创建/更新所需的某些变量,相比于BeanShell 取样器,JSR223取样器+groovy脚本具有可大大提高性能的功能(编译),同时支持更多的脚本语言下面是获取请求签名代码import hashlib import t...
            0 0 799
            分享
      • 51testing软件测试圈微信