数据库基本操作8——子查询

一,概念

含义:出现在其他语句中的select语句,称之为子查询或者内查询

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

分类:

按子查询出现的位置:

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

from后面——支持表子查询

where或者having后面——不支持表子查询

exists后面(相关子查询)——表子查询

按结果集的行列数不同:

标量子查询(一行一列)

列子查询(一列多行)

行子查询(一行多列)

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

特点:

1,子查询放在小夸号内

2,一般放在条件的右侧

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

4,列子查询搭配多行操作符使用and or

二:具体分析

1,标量子查询

案例:查询谁的工资比Abel高

select last_name, salary

from employees

where salary >(

select salary

from employees

where last_name = 'Abel'

);

案例:返回job_id与141号员工相同,salary比143号高的员工姓名,job_id 和工资

select job_id

from employees

where employee_id = 143

select last_name,job_id,salary

from employees

where (

select job_id

from employees

where employee_id = 141)

and salary>(

select salary

from employees

where employee_id = 143);

猜你喜欢

转载自blog.csdn.net/weixin_44841312/article/details/105856075