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