複雜的查詢通常先子查詢,然後巢狀
11.查詢每門課的平均成績
關鍵字 avg()
單門課:
SELECT c_no,AVG(sc_degree) FROM SCORE where c_no = '3-105';
每門課:
SELECT c_no,AVG(sc_degree) FROM SCORE GROUP BY c_no;
12.查詢score表中至少有2名學生選修的,並且以3開頭的課程的平均分
SELECT AVG(sc_degree),c_no from score GROUP BY c_no HAVING COUNT(c_no) >= 2 AND c_no like '3%' ;
13.查詢分數大於70但是小於90的s_no列:
SELECT s_no,sc_degree FROM score WHERE sc_degree BETWEEN 71 AND 89;
14.查詢所有的學生 s_name , c_no, sc_degree列(多表查詢)
SELECT s_name, c_no, sc_degree FROM student,score WHERE student.s_no = score.s_no;
15.查詢所有學生的s_no, c_name, sc_degree列(多表查詢)
SELECT student.s_no, c_name, sc_degree FROM student, course,score WHERE student.s_no = score.s_no AND score.c_no = course.c_no ;
17.查詢班級是’95031’班學生每門課的平均分(子查詢,分組求平均)
select c_no,AVG(sc_degree) from score WHERE s_no IN (select s_no from student where s_class = '95031') GROUP BY c_no;
18.查詢選修"3-105"課程的成績高於’109’號同學’3-105’成績 的所有同學的記錄
SELECT * FROM score WHERE c_no = '3-105' AND sc_degree > (SELECT sc_degree FROM score WHERE s_no = '109' AND c_no = '3-105') ;
19.查詢成績高於學號爲’109’,課程號爲’3-105’的成績的所有記錄
SELECT * FROM score WHERE sc_degree >(SELECT sc_degree FROM score WHERE s_no = '109' AND c_no = '3-105');
20.查詢所有學號爲108.101的同學同年出生的所有學生的s_no,s_name和s_birthday
SELECT * FROM student WHERE YEAR(s_birthday) IN (SELECT YEAR(s_birthday) FROM student WHERE s_no IN('108','101'));
21.查詢 張旭 教師任課的學生的成績(多層巢狀子查詢).
select * from student where s_no IN (SELECT s_no FROM score WHERE c_no = (SELECT c_no FROM course WHERE t_no = (SELECT t_no FROM teacher WHERE t_name='張旭')));
22.查詢選修課程的同學人數多餘 5 人的教師姓名
SELECT * FROM teacher WHERE t_no IN(SELECT t_no FROM course WHERE c_no IN (SELECT c_no FROM score GROUP BY c_no HAVING COUNT(s_no) > 5));
23.查詢95033班和95031班全體學生的記錄(in用法)
SELECT * FROM student WHERE s_class IN('95031','95033') ORDER BY s_class
24.查詢存在85分以上成績的課程c_no
SELECT * FROM score where sc_degree > 85;
25.查出所有’計算機系’ 教師所教課程的成績表
SELECT * FROM score WHERE c_no IN (SELECT c_no FROM course WHERE t_no IN (SELECT t_no FROM teacher WHERE t_depart = '計算機系'));