在網上看見幾個sql練習的題目,又搜了一些其他的解法,寫一下自己的理解。
創建四張表:Course(課程)、SC(成績)、Student(學生)、Teacher(老師)
Student中包括sid、sname、sage、ssex;
SC中包括sid、cid、score;
我的思路比較簡單,從SC中提出sid、AVG(score)到t1,再連接到Student
1 SELECT t1.sid AS 學號,sname AS 姓名,score AS 平均分 FROM 2 (SELECT sid,AVG(score) AS score from SC GROUP BY sid HAVING AVG(score) < 60)t1 3 LEFT JOIN Student ON t1.sid = Student.sid
結果如下:
各位若有建議與疑問,歡迎提出討論;若有什么不對之處,歡迎批評指正。