1. 程式人生 > >mysql作業

mysql作業

order 十分 sum col 次數 limit 連表 from nbsp

1、查詢所有的課程的名稱以及對應的任課老師姓名
select cname,tname from course left join teacher on course.teacher_id=teacher.tid;
2、查詢學生表中男女生各有多少人
select gender,count(sid) from student group by gender;
3、查詢物理成績等於100的學生的姓名
#子查詢的方式
select sname from student where sid in
(
select student_id from score
    where course_id = (select cid from course where cname=‘物理‘) and num=100
);
#連表的方式
select sname from student inner join
(
select student_id from score
    where course_id = (select cid from course where cname=‘物理‘) and num=100
) as a
on a.student_id=student.sid
;

4、查詢平均成績大於八十分的同學的姓名和平均成績
select student.sname,t1.平均成績 from student inner join
(select student_id,avg(num) 平均成績 from score group by student_id having avg(num) > 80) as t1
on student.sid=t1.student_id;
5、查詢所有學生的學號,姓名,選課數,總成績
select student.sid,sname 學生名,選課數,總成績 from student left join
(select student_id,count(course_id) 選課數,sum(num) 總成績 from score group by student_id) as t1
on student.sid=t1.student_id
;
6、 查詢姓李老師的個數
select count(tid) from teacher where tname like ‘李%‘;

7、 查詢沒有報李平老師課的學生姓名
select sname from student where sid not in (
select distinct student_id from score where course_id in (
select cid from course where teacher_id=(select tid from teacher where tname=‘李平老師‘)
)
);
8、 查詢物理課程比生物課程高的學生的學號
select t1.student_id from
(select student_id,num from score inner join course on score.course_id=course.cid
 where course.cname=‘物理‘) as t1
 inner join
(select student_id,num from score inner join course on score.course_id=course.cid
where course.cname=‘生物‘) as t2
on t1.student_id=t2.student_id
where t1.num > t2.num
;
9、 查詢沒有同時選修物理課程和體育課程的學生姓名
select sname from student where sid in (
select student_id from score inner join cour
on course.cname in (‘物理‘,‘體育‘) and course.cid=score.course_id
group by student_id having count(course_id) !=2
);
10、查詢掛科超過兩門(包括兩門)的學生姓名和班級名字
select t2.sname,class.caption from
(select sname,class_id from student inner join (
select student_id from score
where num 
< 60 group by student_id having count(course_id) >=2 ) as t1 on student.sid=t1.student_id) as t2 inner join class on class.cid = t2.class_id ; 11 、查詢選修了所有課程的學生姓名 select sname from student inner join ( select student_id from score group by student_id having count(course_id) = (select count(cid) from course) ) t1 on t1.student_id = student.sid ; 12、查詢李平老師教的課程的所有成績記錄 select student_id,course_id,num from score inner join ( select cid from course inner join teacher on teacher.tname=‘李平老師‘ and teacher.tid=course.teacher_id ) as t1 on t1.cid=score.course_id ; 13、查詢全部學生都選修了的課程號和課程名 select course.cid,course.cname from course inner join ( select course_id from score group by course_id having count(student_id) = (select count(sid) from student) ) t1 on t1.course_id=course.cid ; 14、查詢每門課程被選修的次數 select course.cname,選修人數 from course inner join ( select course_id,count(student_id) as 選修人數 from score group by course_id ) as t1 on t1.course_id=course.cid ; 15、查詢之選修了一門課程的學生姓名和學號 select sid,sname from student inner join ( select student_id from score group by student_id having count(course_id)=1 ) t1 on t1.student_id = student.sid ; 16、查詢所有學生考出的成績並按從高到低排序(成績去重) select distinct num from score order by num desc; 17、查詢平均成績大於85的學生姓名和平均成績 select student.sname,avg_num from student inner join ( select student_id,avg(num) as avg_num from score group by student_id having avg(num) > 85 ) t1 on student.sid=t1.student_id ; 18、查詢生物成績不及格的學生姓名和對應生物分數 select student.sname,t1.num from student inner join ( select student_id,num from score where course_id=(select cid from course where cname=‘生物‘) and num
< 60 ) t1 on t1.student_id=student.sid ; 19、查詢在所有選修了李平老師課程的學生中,這些課程(李平老師的課程,不是所有課程)平均成績最高的學生姓名 select sname from student where sid = ( select student_id from score where course_id in ( select cid from course inner join teacher on teacher.tname=‘李平老師‘ and course.teacher_id=teacher.tid ) group by student_id order by avg(num) desc limit 1 );

mysql作業