0
点赞
收藏
分享

微信扫一扫

【尚硅谷】子查询

迪莉娅1979 2022-01-31 阅读 64
sql数据库

含义:出现在其他语句的select语句,成为子查询或内查询

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

分类:

按子查询出现的位置:

select后面

         仅仅支持标量子查询

from后面

         支持表子查询

where或having后面       

        标量子查询、(单行子查询)

        列子查询、(多行子查询)

        行子查询(多列多行)

【特点:

1.子查询放在小括号内

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

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

> < >= <= <>

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

in any/some all

返回多行。

操作符含义
in/not in等于列表中的任意一个
any/some和子查询返回的某一个值比较
all和子查询返回的所有值比较

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

exists后面(相关子查询)

表子查询

按结果集的行列数不同:

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

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

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

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

返回公司工资最少的员工last_name,job_id,salary

1.查询公司的最低工资

select min(salary)

from employees

2.查询last_name,job_id和salary,要求salary=1

select last_name,job_id,salary

from employees

where salary=(

select min(salary)

from employees

);

返回location_id是1400或1700的部门中的所有员工姓名

1.查询location_id是1400或1700的部门编号

select distinct department_id

from departments

where location_id in(1400,1700)

2.查询员工姓名,要求部门号是1列表中的某一个

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'工种任一工资低的员工号、姓名、job_id以及salary

1.查询job_id为'it_prog'部门任一工资

select distinct salary

from employees

where job_id='it_prog'

2.查询员工号、姓名、job_id以及salary,salary<1的任一一个

select last_name,employee_id,job_id,salary

from employees

where salary<any(

select distinct salary

from employees

where job_id='it_prog'

)and job_id<>'it_prog';

或者:

select last_name,employee_id,job_id,salary

from employees

where salary<(

select max(salary)

from employees

where job_id='it_prog'

)and job_id<>'it_prog';

查询员工编号最小并且工资最高的员工信息

select *

from employees

where(employee_id,salary)=(

        select min(employee_id),max(salary)

        from employees

);

1.查询最小的员工编号

select min(employee_id)

from employees

2.查询最高工资

select max(salary)

from employees

3.查询员工信息

select *

from employees

where employee_id=(

        select min(employee_id)

        from employees

)and salary=(

        select max(salary)

        from employees

);

查询每个部门的员工个数

select d.*,(

select count(*)

from employees

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

)部门名;

查询每个部门的平均工资的工资等级

1.查询每个部门的平均工资

select avg(salary),department_id

from employees

group by department_id

select *

from job_grades;

2.连接1的结果集和job_grades表,筛选条件平均工资between lowest_sal and highest_sal

select ag_dep.*,'grade_level'

from(

select avg(salary) ag,department_id

from employees

group by department_id

) ag_dep

inner join job_grades g

on ag_dep.ag between lowest_sal and highest_sal;

语法:

exists(完整的查询语句)

结果:1或0

查询有员工的部门名

select department_name

from departments d

where exists(

        select *

        from employees e

        where d.department_id=e.department_id

);

select department_name

from departments d

where d.department_id in(

        select department_id

        from employees

)

举报

相关推荐

0 条评论