本次試題設計兩個表:student、exam函數
student表spa
exam表設計
1、寫一條SQL語句,按學號排序輸出數學成績code
SELECT s.sno sno,score FROM exam e,student s WHERE e.`subject` = '數學' AND e.name = s.name GROUP BY s.sno
2、寫一條SQL語句,按學號排序,輸出:學號,姓名,總分,平均分blog
SELECT sno,s.`name`,SUM(score) ss,AVG(score) gg FROM exam e,student s WHERE e.`name` = s.`name` GROUP BY `name` ORDER BY ss
3、寫一條SQL語句,查詢出數學考試成績最高的學生學號和姓名排序
SELECT sno,exam.name name,score FROM exam,student WHERE (exam.`subject` = '數學') AND (exam.name = student.name) ORDER BY exam.score DESC LIMIT 0,1;
4、寫一條SQL語句,查詢出每門課都大於80分的學生姓名索引
方法一:ALL()函數數學
思路:查找出每一個人全部科目是否都大於80class
SELECT DISTINCT NAME FROM exam a WHERE 80 < ALL(SELECT score FROM exam e WHERE e.name = a.name );
方法二:not exists 字段方法
思路:排除掉存在學科分數低於80的學生,剩下的就是全部學科分數都大於80的學生
SELECT DISTINCT NAME FROM exam a WHERE NOT EXISTS (SELECT name FROM exam e WHERE e.name = a.name AND e.score < 80 );
方法三:not in 字段
思路:同上
SELECT DISTINCT NAME FROM exam WHERE NAME NOT IN (SELECT NAME FROM exam WHERE score<=80)
方法四:having 字段
思路:過濾出學科中最低分數大於80的學生
SELECT name FROM exam GROUP BY name HAVING min(score) > 80
5、張三退學後,請寫相關的SQL語句,注意嚴謹
DELETE FROM exam WHERE name = '張三'; DELETE FROM student WHERE name = '張三';
DELETE exam,student FROM exam LEFT JOIN student ON exam.name = student.name WHERE exam.name = '張三' ;
6、你會對該表哪些字段作索引
id - 惟一索引
7、這兩個表的設計,有哪些能夠改進的地方
student表中sno字段能夠改爲‘studentID’,更語義化,
exam表中取消name字段及該列數據,改用student中sno學號字段代替,更易於檢索