练习题(二)Mysql面试笔试练习题
17、查询出只选修了一门课程的全部学生的学号和姓名
SELECT s.student_id,s.student_name FROM student s,score sc WHERE s.student_id =sc.student_id GROUP BY s.student_id HAVING COUNT(s.student_id)
18、查询没有学全所有课的同学的学号、姓名;
(1)SELECT COUNT() FROM course)
(2)SELECT s.student_id FROM score s GROUP BY s.student_id HAVING COUNT(s.course_id) =(SELECT COUNT() FROM course)
(3)SELECT * FROM student t WHERE t.student_id NOT IN (
SELECT s.student_id FROM score s GROUP BY s.student_id HAVING COUNT(s.course_id) =(SELECT COUNT(*) FROM course))
19、查询重名的学生名单,并统计出重名人数
SELECT s.student_name,COUNT() FROM student s GROUP BY s.student_name HAVING COUNT()>1
20、查询平均成绩大于80的所有学生的学号,姓名和平均成绩
SELECT s.student_id,sc.score FROM student s,score sc WHERE s.student_id =sc.student_id GROUP BY s.student_id HAVING AVG(sc.score) >80
21、查询课程名称为“政治”,且分数低于60的学生姓名和分数
SELECT s.student_id,sc.score FROM student s,score sc,course c WHERE s.student_id =sc.student_id AND c.course_id=sc.course_id AND c.course_name=‘政治’ AND sc.score <60
或者:
SELECT s.student_id,sc.score FROM course c JOIN score sc ON c.course_id=sc.course_id JOIN student s ON sc.student_id=s.student_id where c.course_name=‘政治’ AND sc.score <60
22、查询任何一门课程成绩在70分以上的同学姓名、课程名称和分数;
SELECT s.student_id,c.course_name,sc.score FROM student s,score sc ,course c where s.student_id =sc.student_id and sc.course_id=c.course_id AND sc.score >70
23、查询所有不及格课程的课程编号、课程名称、学生姓名、分数,并按课程编号从大到小排列
SELECT c.course_id,c.course_name,s.student_name,sc.score FROM student s,score sc ,course c where s.student_id =sc.student_id and sc.course_id=c.course_id AND sc.score <60 ORDER BY c.course_id DESC
24、查询课程编号为c003且课程成绩在80分以上的学生的学号,姓名,分数;
SELECT s.student_id,s.student_name,sc.score FROM student s,score sc ,course c where s.student_id =sc.student_id and sc.course_id=c.course_id AND sc.score >80 AND c.course_id =‘c003’
25、查询选修“teacher001”老师所授课程的学生中,成绩最高的学生姓名及其成绩
SELECT * FROM
(
SELECT c.course_id,c.course_name,s.student_name,sc.score FROM student s ,score sc ,course c,teacher t WHERE s.student_id = sc.student_id
AND sc.course_id= c.course_id AND c.teacher_id = t.teacher_id AND t.teacher_name = ‘teacher001’ ORDER BY sc.score DESC
)b
GROUP BY b.course_id
或者:
SELECT c.course_id,c.course_name,s.student_name,sc.score FROM student s ,score sc ,course c,teacher t WHERE s.student_id = sc.student_id
AND sc.course_id= c.course_id AND c.teacher_id = t.teacher_id AND t.teacher_name=‘teacher001’
AND sc.score =(SELECT MAX(score) FROM score WHERE course_id =c.course_id)