前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >MySQL 嵌套查询_嵌套查询和嵌套结果的区别

MySQL 嵌套查询_嵌套查询和嵌套结果的区别

作者头像
全栈程序员站长
发布2022-09-22 17:06:39
发布2022-09-22 17:06:39
4.3K0
举报

大家好,又见面了,我是你们的朋友全栈君。

自测题:

1、查询哪些课程没有人选修列出课程号和课程名;

[code]select cno,cname

from course

where cno not in(

select distinct cno

from sc)[/code]

2、用子查询实现如下查询:

(1)查询选修了1号课程的学生姓名和所在系;

[code]select sname,sno

from student

where sno in(

select sno

from sc

where cno=1)[/code]

(2)查询“数据库”成绩在80分以上的学生的学号和姓名;

[code]Select sno,sname

From student

Where sno in(

select sno

from course,sc

where course.cno=sc.cno and course.cname=’数据库’ and grade>=80)[/code](3)查询计算机系最高成绩。

[code]select top 1 grade

from student,sc

where student.sno=sc.sno and sdept=’CS’

order by grade desc[/code]

3、查询同时选修了1号和2号课程的学生学号

[code]select sno

from sc

where cno=1 and sno in(

select sno

from sc

where cno=2)[/code]

4、查询选修了“离散数学”的学生姓名(连接查询)

[code]select sname

from student

where sno in(

select sno

from course,sc

where course.cno=sc.cno and course.cname=’离散数学’)[/code]

5、查询选修课程名为“数据库”的学生姓名(子查询)

[code]select sname

from student

where sno in(

select sno

from course,sc

where course.cno=sc.cno and course.cname=’数据库’)[/code]

6、查询与张天和张琪在同一个系的学生

[code]select *

from student

where sdept in(

select sdept

from student

where sname=’张天’ or sname=’张琪’)[/code]

查询与张天或张琪不在同一个系的学生

[code]select *

from student

where sdept not in(

select sdept

from student

where sname=’张天’ or sname=’张琪’)[/code]

7、查询比信息系所有学生年龄大的学生姓名

[code]select sname

from student s1

where s1.sage>all(

select sage

from student s2

where s2.sdept=’CS’)[/code]

8、查询比张天平均成绩高的学生姓名

[code]select sname

from student

where student.sno in(

select sno

from sc

group by sno

having avg(grade) >(

select avg(grade) as avg_grade2

from sc sc2,student

where student.sno=sc2.sno and sname=’刘晨’

group by sc2.sno)

)[/code]9、查询比学号为200215121学生年龄大的学生

[code]select *

from student s1

where s1.sage>(

select sage

from student s2

where s2.sno=’200215121′)[/code]

10、查询各系总分最高的学生学号

[code]Select sdept,student.sno

from student,sc

where student.sno=sc.sno

group by sdept,student.sno

having sum(grade)>=all(

select sum(grade)

from student,sc

where student.sno=sc.sno and sdept=student.sdept

group by student.sno)[/code]

11、查询选修了以6号课程为先行课的所有课程的学生学号。

[code]select distinct sno

from sc

where sc.cno in(

select cno

from course

where cpno=6)[/code]

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/169407.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档