1. 程式人生 > >(二)Mysql面試筆試練習題

(二)Mysql面試筆試練習題

最常用聚合函式:
AVG() 平均值
COUNT() 計數
MAX() 最大值
MIN() 最小值
SUM() 求和
6、查詢平均成績大於60分的同學的學號和平均成績
SELECT s.student_id,AVG(s.score) FROM score s GROUP BY s.student_id HAVING AVG(s.score)>60;
說明:HAVING 經常跟 GROUP BY 分組過濾一起用

7、查詢每個同學的最高分是多少
SELECT s.student_id,SUM(s.score) FROM score s GROUP BY s.student_id

8、查詢各個課程的名稱及相應的選修人數
SELECT c.course_id,COUNT()FROM course c,score s WHERE c.course_id=s.course_id GROUP BY s.course_id
擴充套件題:
9、查詢選修人數大於4的各個課程的名稱及相應的選修人數
SELECT c.course_id,COUNT(

)FROM course c,score s WHERE c.course_id=s.course_id GROUP BY s.course_id HAVING COUNT(*)>4

10、查詢所有同學的學號、姓名、選課數、總成績;
SELECT s.student_id,s.student_name,COUNT(sc.student_id),SUM(sc.score) FROM student s LEFT JOIN score sc ON s.student_id =sc.student_id GROUP BY s.student_id

11、查詢“c001”課程比“c002”課程成績高的所有學生的學號;
技巧:
(1)先查出"c001"課程所有學生的學號,成績
SELECT student_id,course_id,score FROM score WHERE course_id = ‘c001’
(2)先查出"c002"課程所有學生的學號,成績
SELECT student_id,course_id,score FROM score WHERE course_id = ‘c002’
(3)然後在合在一起,最終結果:
SELECT a.student_id,a.score,a.course_id,b.course_id,b.score FROM (
SELECT student_id,course_id,score FROM score WHERE course_id = ‘c001’
)a,
(
SELECT student_id,course_id,score FROM score WHERE course_id = ‘c002’
)b
WHERE a.student_id = b.student_id AND a.score > b.score

12、查詢沒學過“teacher001”老師課的同學的學號、姓名;
技巧:
(1)先查出學過"teacher001"老師課的同學的學號、姓名
SELECT * FROM course c,score s,teacher t WHERE c.teacher_id= t.teacher_id AND s.course_id=s.course_id AND t.teacher_name=‘teacher001’
(2)然後在用not in 就是沒學過的
SELECT t.* FROM student t WHERE t.student_id NOT IN
(SELECT DISTINCT(s.student_id) FROM course c,score s,teacher t WHERE c.teacher_id= t.teacher_id AND s.course_id=s.course_id AND t.teacher_name=‘teacher001’)

13、查詢學過“c001”並且也學過編號“c002”課程的同學的學號、姓名;

SELECT s.student_id,s.student_name FROM student s,course c,score sc WHERE s.student_id=sc.student_id AND sc.course_id =c.course_id AND c.course_id=
( SELECT sc2.student_id FROM score AS sc2 WHERE sc2.student_id =s.student_id AND sc2.course_id =‘c002’ )

用in的方式:
SELECT s.student_id,s.student_name FROM student s ,score sc WHERE s.student_id= sc.student_idAND sc.course_id = ‘c001’ AND s.student_id IN
(
SELECT sc2.student_id FROM score AS sc2 WHERE sc2.student_id = s.student_id AND sc2.course_id= ‘c002’
)
14、查詢“c001”課程比“c002”課程成績高的所有學生的學號、姓名;
SELECT
sc3.student_id,sc3.student_name
FROM
(SELECT
s.student_id,
s.student_name,
sc.score,
(SELECT sc2.score FROM score sc2 WHERE sc2.student_id = s.student_id AND sc2.course_id = ‘c002’) score2
FROM student s,score sc WHERE s.student_id= sc.student_id AND sc.course_id = ‘c001’) sc3
WHERE sc3.score > sc3.score2

15、查詢所有課程成績小於60分的同學的學號、姓名
SELECT student_id,student_name FROM student WHERE student_id NOT IN (
SELECT s.student_id FROM student s,score c WHERE s.student_id = c.student_id AND c.score >60
)

16、查詢至少有一門課與學號為“s001”的同學所學相同課程的同學的學號和姓名;
SELECT DISTINCT s.student_id,s.student_name FROM student s,score sc WHERE s.student_id =sc.student_id AND sc.course_id in
(SELECT sc.course_id FROM score sc WHERE sc.student_id =‘s001’)

17、查詢每門課程被選修的學生數
SELECT c.course_id,COUNT(*) FROM course c GROUP BY c.course_id;