SQL多表查詢練習題


搜集自網上資料

查詢所有學生的學號、姓名、選課數、總成績
select a.學號,a.姓名,count(b.課程號) as 選課數,sum(b.成績) as 總成績from student as a left join score as bon a.學號 = b.學號group by a.學號;

查詢平均成績大於85的所有學生的學號、姓名和平均成績
select a.學號,a.姓名, avg(b.成績) as 平均成績from student as a left join score as bon a.學號 = b.學號group by a.學號having avg(b.成績)>85;

查詢學生的選課情況:學號,姓名,課程號,課程名稱
select a.學號, a.姓名, c.課程號,c.課程名稱
from student a inner join score b on a.學號=b.學號
inner join course c on b.課程號=c.課程號;

查詢出每門課程的及格人數和不及格人數
-考察case表達式
select 課程號,sum(case when 成績>=60 then 1 else 0 end) as 及格人數,sum(case when 成績 < 60 then 1 else 0 end) as 不及格人數from scoregroup by 課程號;

使用分段[100-85],[85-70],[70-60],[<60]來統計各科成績,分別統計:各分數段人數,課程號和課程名稱
考察case表達式
select a.課程號,b.課程名稱,sum(case when 成績 between 85 and 100 then 1 else 0 end) as '[100-85]',sum(case when 成績 =70 and 成績<85 then 1 else 0 end) as '[85-70]',sum(case when 成績>=60 and 成績<70 then 1 else 0 end) as '[70-60]',sum(case when 成績<60 then 1 else 0 end) as '[<60]'from score as a right join course as b on a.課程號=b.課程號group by a.課程號,b.課程名稱;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM