MySQL经典练习题(三)

1、查询“张旭“教师任课的学生成绩。

-- 方法一

select *

from score

where cno = (select cno from course where tno =(select tno from teacher where tname ='张旭'))

-- 方法二

select sc.*

from teacher t , course c, score sc

where t.tno = c.tno and c.cno = sc.cno and t.tname ='张旭'

-- 方法三

select sc.*

from teacher t join course c join score sc

on t.tno = c.tno and c.cno = sc.cno

where t.tname ='张旭'

-- 方法四

select sc.*

from teacher t inner join course c inner join score sc

on t.tno = c.tno and c.cno = sc.cno

where t.tname ='张旭'

-- 方法五

select sc.*

from teacher t inner join course c inner join score sc

on t.tno = c.tno and c.cno = sc.cno and t.tname ='张旭'

2、查询选修某课程的同学人数多于5人的教师姓名。

-- 方法一

select tname

from teacher

where tno in (select tno from course where cno in (select cno from  score group by CNO having count(cno) > 5))

-- 方法二

select t.tname

from teacher t join course c join score sc

on t.tno = c.tno and c.cno = sc.cno

group by sc.CNO

having count(sc.cno) > 5

-- 方法三

select t.tname

from teacher t inner join course c inner join score sc

on t.tno = c.tno and c.cno = sc.cno

group by sc.CNO

having count(sc.cno) > 5

-- 方法四

select t.tname

from teacher t , course c , score sc

where t.tno = c.tno and c.cno = sc.cno

group by sc.CNO

having count(sc.cno) > 5

3、查询95033班和95031班全体学生的记录。

-- 方法一

select *

from student

where class = '95033' or class ='95031'

-- 方法二

select *

from student

where class in ('95033' ,'95031')

4、查询存在有85分以上成绩的课程Cno。

-- 方法一

select cno

from score

where degree >=85

group by cno

-- 方法二

select DISTINCT cno

from score

where degree >=85

5、查询所有教师和同学的name、sex和birthday

-- 方法一

select distinct Sname as name,Ssex as sex,Sbirthday as birthday from student

union

select distinct Tname as name,Tsex as sex,Tbirthday as birthday from Teacher

-- 方法二

select tname name,tsex sex, tbirthday birthday

from teacher

union

select sname name,ssex sex, sbirthday birthday

from student

6、查询所有“女”教师和“女”同学的name、sex和birthday。

-- 方法一

select tname name,tsex sex, tbirthday birthday

from teacher

where tsex ='女'

union

select sname name,ssex sex, sbirthday birthday

from student

where ssex ='女'

-- 方法二

select tname as name,tsex as sex, tbirthday as birthday from teacher where tsex ='女'

union

select sname as name,ssex as sex, sbirthday as birthday from student where ssex ='女'

7、查询成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree

-- 方法一

select cno,sno,degree

from score

where degree > (select min(degree) from score where cno ='3-245') and cno !='3-245'

order by degree desc

-- 方法二

select cno,sno,degree

from score

where degree > (select min(degree) from score where cno ='3-245') and cno <>'3-245'

order by degree desc

8、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。

select cno ,sno,degree

from score

where degree > (select min(degree) from score where cno ='3-245') and cno ='3-105'

order by degree desc

9、查询“计算机系”中的与“电子工程系“不同职称的教师的Tname和Prof。

select tname,prof

from teacher

where prof not in (select prof from teacher where depart='电子工程系' and prof in (select prof from teacher where depart='计算机系'))

and depart in ('计算机系','电子工程系')

10、查询出“计算机系“教师所教课程的成绩表

-- 方法一

select *

from score

where cno in (select cno from course where tno in (select tno from teacher where depart='计算机系' ))

-- 方法二

select sc.*

from score sc ,teacher t ,course c

where t.tno =c.tno and c.cno = sc.cno and t.depart='计算机系'

欢迎批评指正,提出问题,谢谢!
原文地址:https://www.cnblogs.com/xxeleanor/p/14941764.html