sql语句之not exists的三种使用场景


--查询没有选修102号课程的学生的学生和姓名
select SNO,SNAME
from S
where not exists(	
	select *
	from SC
	where(
		S.SNO=SNO and CNO='102'
	)
)

--查询选修了所有课程的学生的学号和姓名
select SNO,SNAME
from S
where not exists(
	select *
	from C
	where not exists(
		select *
		from SC
		where SNO=S.SNO and C.CNO=CNO
	)
)

--查询选修了001号同学选修过的所有课程的学生的学号和姓名
select SNO,SNAME
from S
where not exists(
	select *
	from SC sc1
	where SNO='001' and not exists(
		select *
		from SC sc2
		where(
			SNO=S.SNO and sc1.CNO=CNO
		)
	)
)

猜你喜欢

转载自blog.csdn.net/jiuweideqixu/article/details/88213368
今日推荐