9.mysql SQL面試題
Student(S#,Sname,Sage,Ssex) 學生表 S#:學號;Sname:學生姓名;Sage:學生年齡;Ssex:學生性別 Course(C#,Cname,T#) 課程表 C#,課程編號;Cname:課程名字;T#:教師編號 SC(S#,C#,score) 成績表 S#:學號;C#,課程編號;score:成績 Teacher(T#,Tname) 教師表 T#:教師編號; Tname:教師名字
問題:
1、查詢“001”課程比“002”課程成績高的所有學生的學號;
(select s#,score from SC where C#='001') a,
(select s#,score from SC where C#='002') b where a.score>b.score and a.s#=b.s#;
2、查詢平均成績大於60分的同學的學號和平均成績; select S#,avg(score) from sc group by S# having avg(score) >60;
3、查詢沒學過“葉平”老師課的同學的學號、姓名; select Student.S#
4、查詢學過“001”並且也學過編號“002”課程的同學的學號、姓名; select Student.S#,Student.Sname from Student join SC on Student.S#=SC.S# where SC.C#='001'
5、查詢課程編號“002”的成績比課程編號“001”課程低的所有同學的學號、姓名;
SELECT sid, sname from ( SELECT student.sid,student.sname,score, (SELECT score from sc as sc2 where sc2.sid = sc.sid and sc2.cid = 2) as score2 from student join sc on student.sid = sc.sid WHERE cid = 1 ) t WHERE t.score > t.score2
6、查詢所有課程成績小於60分的同學的學號、姓名;(排除掉有分數及格的學生的學號) select S#,Sname from Student where S# not in (select Student.S# from Student,SC where S.S#=SC.S# and score>60);
7、檢索至少選修兩門課程的學生學號 select S# from sc group by s# having count(*) > = 2
8、查詢兩門以上不及格課程的同學的學號及其平均成績
select S#,avg(score) from SC where S# in ( select S# from SC where score <60 group by S# having count(*)>2 ) group by S#;
9、查詢選修“葉平”老師所授課程的學生中,成績最高的學生姓名及其成績
select Student.Sname,score from Student,SC,Course C,Teacher where Student.S#=SC.S# and SC.C#=C.C# and C.T#=Teacher.T# and Teacher.Tname='葉平' and SC.score=(select max(score)from SC where C#=C.C# );