1. 選出每門功課都及格的學號 select distinct `s#` from sc where `s#` not in (select `s#` from sc where score 60) 2. 查詢“1”課程比“2”課程成績高的所有學生的學號; SELECT c01.`s#` from (select `s#`, `score` from sc where `c#`=1) c01, (select `s#`, `score` from sc where `c#`=2) c02 where c01.`s#` = c02.`s#` and c01.score > c02.score 3. 查詢平均成績大于60分的同學的學號和平均成績; select `s#`, avg(score) from sc group by `s#` having avg(score) > 60 4. 查詢所有同學的學號、姓名、選課數(shù)、總成績; select student.`s#`, student.`Sname`, count(`c#`), sum(score) from student left outer join sc on student.`s#` = sc.`s#` group by student.`s#`, sc.`s#`
5.查詢沒學過“葉平”老師課的同學的學號、姓名; select student.`s#`, student.`Sname` from student where student.`s#` not in (select distinct(sc.`s#`) from teacher, course, sc where Tname='葉平' and teacher.`t#` = course.`t#` and sc.`c#`= course.`c#` ) 6. 查詢學過“001”并且也學過編號“002”課程的同學的學號、姓名 select student.`s#`, student.sname from student, sc where student.`s#` = sc.`s#` and sc.`c#` = 1 and exists (select * from sc sc_2 where sc_2.`c#`=2 and sc.`s#`=sc_2.`s#`) 7. 查詢學過“葉平”老師所教的所有課的同學的學號、姓名 (巧妙) select `s#`, sname from student where `s#` in (select `s#` from sc, teacher, course where tname='葉平' and teacher.`t#`=course.`t#` and course.`c#`= sc.`c#` group by `s#` having count(sc.`c#`)= (select count(`c#`) from teacher, course where tname='葉 平' and teacher.`t#`=course.`t#`) )
8. 查詢課程編號“002”的成績比課程編號“001”課程低的所有同學的學號、姓名 (有代表性) select `s#`, sname from (select student.`s#`, student.sname, score, (select score from sc sc_2 where student.`s#`=sc_2.`s#` and sc_2.`c#`=2) score2 from student , sc where sc.`s#`=student.`s#` and sc.`c#`=1) s_2 where score2 score 9.查詢沒有學全所有課的同學的學號、姓名 select student.`S#`, Sname from student, sc where student.`s#` = sc.`s#` group by `s#`, sname having count(`c#`) (select count(`c#`) from course)
10. 查詢至少有一門課與學號為“002”的同學所學相同的同學的學號和姓名; select distinct(sc.`s#`), sname from student, sc where student.`s#`=sc.`s#` and `c#` in (select `c#` from sc where `s#`=002) 11. 把“SC”表中“葉平”老師教的課的成績都更改為此課程的平均成績;
update sc inner join (select sc2.`c#`, avg(sc2.score) score from sc sc2, teacher, course where sc2.`c#`=course.`c#` and tname='葉平' and teacher.`t#` = course.`t#` and course.`c#`=sc2.`c#` group by course.`c#`) sc3 on sc.`c#`=sc3.`c#` set sc.score=sc3.score 12. 查詢2號的同學學習的課程他都學了的同學的學號;(注意理解:where語句的 第一個條件過濾掉不滿足c#的記錄,再group by,就比較清晰) select `S#` from SC where `C#` in (select `C#` from SC where `S#`=2) group by `S#` having count(*)=(select count(*) from SC where `S#`=2);