老男孩mysql習題答案匯總


題目:http://www.cnblogs.com/wupeiqi/articles/5729934.html

2、查詢“生物”課程比“物理”課程成績高的所有學生的學號;

3、查詢平均成績大於60分的同學的學號和平均成績;

  select a.sid,avg(number) from student a,score b where a.sid=b.student_id group by a.sid having avg(number) > 60;

4、查詢所有同學的學號、姓名、選課數、總成績;

  select a.sid,sname,count(b.sid) as course_num,sum(b.number) as scores from student a left join score b on a.sid=b.student_id group by a.sid;

5、查詢姓“李”的老師的個數;

  select count(tid) from teacher where tname like '李%';

6、查詢沒學過“葉平”老師課的同學的學號、姓名;

select student.sid,sname from student where student.sid not in (select student_id from score where course_id in (select cid from course,teacher where teacher_id=tid and tname='葉平'));

     查詢學過“葉平”老師課的同學的學號、姓名;

select student_id,sname from score,student where student.sid=student_id and course_id in (select cid from course,teacher where teacher_id=tid and tname='葉平') group by student_id;

7、查詢學過“001”並且也學過編號“002”課程的同學的學號、姓名;

  select s1.sid from student s1 where (select count(*) from score s2 where s2.student_id=s1.sid and s2.course_id=1)>0 and (select count(*) from score s3 where s3.student_id=s1.sid      and s3.course_id=2)>0;

8、查詢學過“葉平”老師所教的所有課的同學的學號、姓名;(若在表score上建立 student_id 和 course_id 的唯一索引)

select student_id,sname from score,student where student_id=student.sidnd course_id in (select cid from course,teacher where teacher_id=tid and tname=波多') group by student_id having count(*)>=2;

9、查詢課程編號“002”的成績比課程編號“001”課程低的所有同學的學號、姓名;

select a.student_id,sname,a.course_id,a.number,b.course_id,b.number from score a, score b,student where a.student_id=b.student_id and a.student_id=student.sid and a.course_id=1 and b.course_id=2 and a.number>b.number;

10、查詢有課程成績小於60分的同學的學號、姓名;

select student_id,sname from score,student where student_id=student.sid and number<60 group by student_id;

11、查詢沒有學全所有課的同學的學號、姓名;

select student.sid,sname from student left join score on student.sid=student_id group by student.sid having count(*)<(select count(*) from course);

12、


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM