學生表:student(學號sno,學生姓名sname,出生年月sbirth,性別ssex)
成績表:score(學號sno,課程號cno,成績score)
課程表:course(課程號cno,課程名稱cname,教師號ctno)
教師表:teacher(教師號tno,教師姓名tname)
注意:下面SQL的實現以MySQL為主
/*
分析思路
select 查詢結果 []
from 從哪張表中查找數據 [成績表score]
where 查詢條件 [課程編號為“04”且分數小于60]
group by 分組 [沒有]
having 對分組結果指定條件 []
order by 對查詢結果排序[查詢結果按按分數降序排列];
*/
select 學號 from score where 課程號='04' and 成績 <60
order by 成績 desc;
select count(教師號) from teacher where 教師姓名 like '孟%';
/*
查找1990年出生的學生名單
學生表中出生日期列的類型是datetime
*/
select 學號,姓名 from student where year(出生日期)=1990;
select sum(成績) from score where 課程號 = '0002';
select count(distinct 學號) as 學生人數 from score;
select 課程號, count(學號) from score group by 課程號;
/*
分析思路
group by 分組 [男生、女生人數:按性別分組
having 對分組結果指定條件 [沒有]
order by 對查詢結果排序[沒有];
*/
select 性別,count(*) from student group by 性別;
select 課程號 from score where 成績<60 order by 課程號 desc;