复杂点的查询

表信息:

 

 

 


--union多表联合查询
select * from Student_Table
union
select * from Employee_Table
union
select * from Teacher_Table
--union 排序
select 学生姓名 as  姓名 from Student_Table where 学生性别=1
union
select 员工姓名  from Employee_Table where 员工性别=0
order by 姓名 desc

--查询两表公共成员
select * from Student_Table
intersect
select * from Employee_Table

--查询两表除公共信息以外的信息
select 学生姓名 from Student_Table
except
select 员工姓名 from Employee_Table

--分组
select 学生姓名,sum(学生编号) as 编号 from Student_Table
group by 学生姓名

--having
select 学生姓名,sum(学生编号) as 编号 from Student_Table
group by 学生姓名
having sum(学生编号)>2

--select 中使用子查询
select
(select avg(学生编号) from Student_Table ) as 编号
from Student_Table

--from中使用子查询
select *
from (select 学生姓名,sum(学生编号) as 编号1,avg(学生编号) as 编号2 from Student_Table group by
学生姓名) tab1
order by 编号1


--where中使用子查询
select *
from Student_Table
where 学生年龄>(select AVG(学生年龄) from Student_Table)

--in中使用子查询
select * from
Student_Table
where 学生年龄 in(select 学生年龄 from Student_Table where 学生年龄 between 20 and 30 )

 

--exists中使用子查询
select * from
Student_Table AS A
where EXISTS(SELECT 学生编号 from Student_Table where 学生编号=A.学生编号)

--any
select * from Student_Table
where 学生年龄>any(select 学生年龄 from Student_Table)

--all
select * from Student_Table
where 学生年龄>all(select 学生年龄 from Student_Table where 学生性别=0)

--子查询嵌套
select * from Student_Table
where 学生年龄>(SELECT AVG(学生年龄) from Student_Table where 出生日期>(select avg(year(出生日期)) from Student_Table ))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值