【SQLServer系列教程】——IN子查询与EXISTS子查询

1.IN子查询语法规范以及应用场景

【SQLServer系列教程】——IN子查询与EXISTS子查询
【SQLServer系列教程】——IN子查询与EXISTS子查询

2.NOT IN子查询

【SQLServer系列教程】——IN子查询与EXISTS子查询

3.EXISTS子查询

【SQLServer系列教程】——IN子查询与EXISTS子查询

4.案例代码:

示例1:IN子查询的使用

use StudentManageDB
go
--select StudentName from Students 
--where StudentId=(select StudentId from ScoreList where SQLServerDB>80)
select StudentName from Students 
where StudentId in (select StudentId from ScoreList where SQLServerDB>80)

示例2:NOT IN子查询的使用

use StudentManageDB
go
select StudentId,StudentName from Students
where StudentId not in(select StudentId from ScoreList)

示例3:EXISTS子查询的使用

use StudentManageDB
go
if exists(select * from ScoreList where CSharp<60)
   print '本次考试内容较难'
else
   print '本次考试内容适中'

--NOT EXISTS的使用
if not exists(select * from ScoreList where CSharp<60)
      print '本次考试内容适中'
else
      print '本次考试内容较难'