MySQL练习(嵌套查询)

1,832 阅读1分钟

(1)查询“金羽”同学所选课程的成绩。

select cno,degree from sc

where sno in (select sno from student where sname=' 金羽 ');

(2)查询“李新”老师所授课程的课程名称。

select cname from course

where cno=(select cno from teaching where tno=(select Tno from teacher where Tname=' 李新 '));

(3)查询女教师所授课程的课程号及课程名称。

select cno,cname from course

where cno in (select cno from teaching where tno in(select Tno from teacher where Tsex=' '));

(4)查询姓“王”的学生所学的课程名称。

select cname from course

where cno in (select cno from sc where sno in (select sno from student where sname like ' %') );

(5)查询“C04”课程不及格的学生信息。

select * from student

where sno in (select sno from sc where cno='c04' and degree<60);

(6)查询选修“高等数学”课程且成绩在80~90分的学生学号及成绩。

select sno,degree from sc

where cno = (select cno from course where cname=' 高等数学 ') and degree between 80 and 90;

(7)查询选修“C04”课程的学生的平均年龄。

select avg(year(curdate())-year(sbirthday)) from student

where sno in (select sno from sc where cno='c04');

(8)查询选修课程名为“高等数学”的学生学号和姓名。

select sno,sname from student

where sno in (select sno from sc where cno=(select cno from course where cname=' 高等数学 '));

(9)查询“钱军”教师任课的课程号,选修其课程的学生的学号和成绩。

select c.cno,sno,degree from teaching b,sc c

where b.cno=c.cno and Tno=(select Tno from teacher where Tname=' 钱军 ');

或者:

SELECT sno,cno,degree FROM sc WHERE cno in

(SELECT cno FROM teaching WHERE tno=(SELECT tno FROM teacher WHERE tname=' 钱军 '))

(10)查询在第1学期所开课程的课程名称及学生的成绩。

select cname,degree from course a,sc b

where a.cno=b.cno and b.cno in (select cno from teaching where cterm=1);

(11)查询与“金羽”同一个班级的同学姓名。

select sname from student

where classno in (select classno from student where sname=' 金羽 ');

(12)查询学号比“金羽”同学大,而出生日期比他小的学生姓名。

select sname from student

where sno> (select sno from student where sname=' 金羽 ')

and year(curdate())-year(sbirthday)< (select year(curdate())-year(sbirthday) from student where sname=' 金羽 ');

(13)查询出生日期大于所有女同学出生日期的男同学的姓名及班级编号。

select sname,classno from student

where sbirthday>all (select sbirthday from student where ssex=' ')

and ssex=' ';

(14)查询不讲授“C01”课的教师姓名。

select Tname from teacher

where Tno not in (select tno from teaching where cno='c01' );

(15)查询没有选修“C02”课程的学生学号及姓名。

select sno,sname from student

where sno not in (select sno from sc where cno='c02');

(16)查询选修了“JAVA程序设计基础”课程的学生学号、姓名及班级编号。

select sno,sname,classno from student

where sno in (select sno from sc

where cno=(select cno from course where cname=' JAVA程序设计基础 '));