50个SQL高级查询练习(6)

题目六、

6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;

select t1.sid as 学号,t3.sname as 姓名 from sc t1,sc t2 ,student t3 where  t1.cid='001'and t2.cid='002' and t1.sid=t2.sid 
and t1.sid=t3.sid;

1、首先查询出既学过“001”并且也学过编号“002”课程的同学的学号;

select t1.sid  from sc t1,sc t2 where  t1.cid='001'and t2.cid='002' and t1.sid=t2.sid ;

查询结果如下:

《50个SQL高级查询练习(6)》

2、然后再和student 进行关联

select t1.sid as 学号,t3.sname as 姓名 from sc t1,sc t2 ,student t3 where  t1.cid='001'and t2.cid='002' and t1.sid=t2.sid 
and t1.sid=t3.sid;

最终查询结果如下:

《50个SQL高级查询练习(6)》

    原文作者:SQL
    原文地址: https://blog.csdn.net/JIAMENGHUI/article/details/80628774
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞