常用資料庫檢索語句
create database DBMonth1 use DBMonth1 create table Student ( SID int primary key identity, Sname varchaR(50), Sage datetime, Ssex varchar(50) ) create table Teacher ( TID int primary key identity, Tname varchar(50), ) create table Course ( CID int primary key identity, Cname varchar(50), TID int foreign key references Teacher(TID) ) create table Score ( ID int primary key identity, SID int foreign key references Student(SID), CID int foreign key references Course(CID), Scores int ) insert into Student values( ‘趙雷’ , ‘1990-01-01’ , ‘男’); insert into Student values( ‘錢電’ , ‘1990-12-21’ , ‘男’); insert into Student values( ‘孫風’ , ‘1990-05-20’ , ‘男’); insert into Student values( ‘李雲’ , ‘1990-08-06’ , ‘男’); insert into Student values( ‘周梅’ , ‘1991-12-01’ , ‘女’); insert into Student values( ‘吳蘭’ , ‘1992-03-01’ , ‘女’); insert into Student values( ‘鄭竹’ , ‘1989-07-01’ , ‘女’); insert into Student values( ‘王菊’ , ‘1990-01-20’ , ‘女’); select * from Student
insert into Teacher values( ‘秦紅山’); insert into Teacher values( ‘李凡’); insert into Teacher values( ‘於彥強’);
select * from Teacher
insert into Course values( ‘語文’ , 2); insert into Course values( ‘數學’ , 1); insert into Course values( ‘英語’ , 3);
insert into Score values(1,1,80); insert into Score values(1,2,56); insert into Score values(1,3,88); insert into Score values(2,1,60); insert into Score values(2,2,70); insert into Score values(2,3,90); insert into Score values(3,1,95); insert into Score values(3,2,86); insert into Score values(3,3,99); insert into Score values(4,1,77); insert into Score values(4,2,44); insert into Score values(4,3,88); insert into Score values(5,1,66); insert into Score values(5,2,85); insert into Score values(5,3,88); insert into Score values(6,1,83); insert into Score values(6,2,88); insert into Score values(6,3,83); insert into Score values(7,2,40); insert into Score values(8,3,90); insert into Score values(8,1,30);
–1、查詢”1”課程比”2”課程成績高的學生的資訊及課程分數 –答: select a.*,b.Scores[課程1的分數], c.Scores[課程2的分數] from Student a,Score b,Score c where a.SID=b.SID and b.SID=c.SID and b.CID=1 and c.CID =2 and b.Scores>c.Scores –.2、查詢同時存在”01”課程和”02”課程的情況和不存在”01”課程但存在”02”課程的情況 –答 –1
–3、查詢平均成績大於等於60分的同學的學生編號和學生姓名和平均成績 –答: select a.Sname[學生姓名],cast(avg(b.Scores) AS decimal(18,2))[平均分] from Student a , Score b where a.SID=b.SID group by a.Sname having cast(avg(b.Scores) AS decimal(18,2))<60 –4、查詢在sc表中不存在成績的學生資訊的SQL語句。 –答: select a.Sname[學生姓名],cast(avg(b.Scores) AS decimal(18,2))[平均分] from Student a left join Score b on a.SID=b.SID group by a.Sname having cast(avg(b.Scores) AS decimal(18,2))<60 –5、查詢所有同學的學生編號、學生姓名、選課總數、所有課程的總成績 –答: select a.Sname[姓名],count(b.CID)[選課總數],SUM(Scores)[總成績] from Student a ,Score b where a.SID=b.SID group by a.Sname
–6、查詢”李”姓老師的數量 –答 select count(Tname) [李姓老師的數量] from Teacher where Teacher.Tname like ‘李%’ –7、查詢學過”秦紅山”老師授課的同學的資訊 –答: select distinct Student.* from Student ,Score,Course,Teacher where Student.SID=Score.SID and Score.CID=Course.CID and Course.TID=Teacher.TID and Teacher.Tname=’秦紅山’
–8、查詢沒學過”秦紅山”老師授課的同學的資訊 –答: select Student.Sname[學生姓名] from Student where SID not in( select distinct SID from Score,Course,Teacher where Score.CID=Course.CID and Course.TID=Teacher.TID and Teacher.Tname=’秦紅山’)
–9、查詢學過編號為”01”並且也學過編號為”02”的課程的同學的資訊 –答: select Sname[學生姓名] from Student where SID IN( select SID from ( select SID from Score where Score.CID=1 union all select SID from Score where Score.CID=2) t group by SID having COUNT(*)=2 ) –10、查詢學過編號為”01”但是沒有學過編號為”02”的課程的同學的資訊 –答:正確的SID是8 這個不會
create view Score2 as select * from Score
select a.Sname[學生姓名] from Student a ,Score b where a.SID=b.SID and b.CID=1 and not exists( select distinct Score.SID from Score ,Score2 where Score.SID=Score2.SID and Score2.CID=2 )
–11、查詢沒有學全所有課程的同學的資訊、 –答: select Student.Sname[學生姓名] from Student ,Score where Student.SID=Score.SID group by Sname having count(CID)<(select count(CID) FROM Course)
–12、查詢和”01”號的同學學習的課程完全相同的其他同學的資訊 –答:錯誤 select Student.Sname[學生姓名] from Student where SID IN ( select * from Score where SID=1 and Score.CID in (select CID from Score where SID=1) group by Score.SID having count(1)=(select count(*) from Score where SID=1))
–14、查詢沒學過”秦紅山”老師講授的任一門課程的學生姓名
select Student .* from Student where SID not in( select Score.SID from Score ,Course,Teacher where Score.CID=Course.CID and Course.TID=Teacher.TID and Teacher.Tname=’秦紅山’)
–15、查詢兩門及其以上不及格課程的同學的學號,姓名及其平均成績
select Student.Sname[學生姓名],cast(AVG(Score.Scores) as decimal(18,2))[平均成績] from Student ,Score where Student.SID=Score.SID and Student.SID in( select SID from Score where Scores >60 group by Score.SID having COUNT(1)>=2 ) group by Student.Sname
–16、檢索”01”課程分數小於60,按分數降序排列的學生資訊
select a.* from Student a ,Score b where a.SID=b.SID and b.Scores<60 and b.CID=1 order by Scores DESC
–17、按平均成績從高到低顯示所有學生的所有課程的成績以及平均成績
select Student .Sname[學生姓名] , max(case Course.Cname when ‘語文’ then Score.Scores else null end)[語文], max(case Course.Cname when ‘數學’ then Score.Scores else null end)[數學], max(case Course.Cname when ‘英語’ then Score.Scores else null end)[英語], cast(avg(Score.Scores) as decimal(18,2)) [平均分] from Student left join Score on Student.SID=Score.SID left join Course on Score.CID=Course.CID group by Student.Sname order by [平均分] desc –18、查詢學生平均成績及其名次
select a.Sname[姓名] , cast(avg(b.Scores) as decimal(18,2))[平均分] ,ROW_NUMBER() over (order by cast(avg(b.Scores) as decimal(18,2)) desc ) as [名次] from Student a join Score b on a.SID=b.SID group by a.Sname
select a.Sname[姓名],cast(avg(b.Scores) as decimal(18,2))[平均分],ROW_NUMBER() over(order by cast(avg(b.Scores) as decimal(18,2)) desc ) as [名次] from Student a , Score b where a.SID=b.SID group by a.Sname –19、查詢各科成績前三名的記錄
–20、查詢每門課程被選修的學生數 select CID ,count(SID)[選擇的人數] from Score group by CID –21、查詢出只有兩門課程的全部學生的學號和姓名 select a.Sname[姓名] from Student a ,Score b where a.SID=b.SID group by a.Sname having count(b.CID)=2
–28、查詢男生、女生人數
select count(Ssex) [男生人數] from Student where Ssex=’男’ select count(Ssex) [女生人數] from Student where Ssex=’女’
select sum(case when Ssex=’男’ then 1 else 0 end )[男生人數] , sum(case when Ssex=’女’ then 1 else 0 end )[女生人數] from Student
select case when Ssex=’男’ then ‘男生人數’ else ‘女生人數’ end [男女情況] ,count (1) [人數] from Student group by case when Ssex=’男’ then ‘男生人數’ else ‘女生人數’ end
–29、查詢名字中含有”風”字的學生資訊 select * from Student where Sname like ‘%’+’風’+’%’
–30、查詢同名同性學生名單,並統計同名人數 select Sname[學生姓名],count(*) [人數] from Student group by Sname having count(*)>1
–31、查詢1990年出生的學生名單(注:Student表中Sage列的型別是datetime)
select *from Student where year(Sage)=1990
select * from Student where datediff(yy,Sage,’1990-01-01’)=0
–32、查詢每門課程的平均成績,結果按平均成績降序排列,平均成績相同時,按課程編號升序排列
select a.CID[課程編號],a.Cname[課程名稱] ,cast(avg(b.scores) as decimal(18,2))[平均分] from Course a ,Score b where a.CID=b.CID group by a.CID,a.Cname order by [平均分] desc,a.CID asc
–33、查詢平均成績大於等於85的所有學生的學號、姓名和平均成績
select a.SID [學生編號],a.Sname[學生姓名],cast(avg(b.Scores)as decimal(18,2))[平均分] from Student a,Score b where a.SID=b.SID group by a.SID,a.Sname having cast(avg(b.Scores)as decimal(18,2))>85
–34、查詢課程名稱為”數學”,且分數低於60的學生姓名和分數 select a.Sname[學生姓名],b.Scores[分數] from Student a,Score b,Course c where a.SID=b.SID and b.CID=c.CID and c.Cname=’數學’ group by a.Sname,b.Scores having b.Scores<60
–36、查詢任何一門課程成績在70分以上的姓名、課程名稱和分數;
select a.Sname[學生姓名],b.Scores[分數],c.Cname[課程名稱] from Student a ,Score b,Course c where a.SID=b.SID and b.CID=c.CID group by a.Sname,b.Scores,c.Cname having b.Scores>70 –37、查詢不及格的課程 select a.Sname[學生姓名],b.Scores[分數],c.Cname[課程名稱] from Student a ,Score b,Course c where a.SID=b.SID and b.CID=c.CID group by a.Sname,b.Scores,c.Cname having b.Scores<60
—-38、查詢課程編號為01且課程成績在80分以上的學生的學號和姓名; select a.Sname[學生姓名],b.Scores[分數] from Student a ,Score b where a.SID=b.SID and b.CID=1 group by a.Sname,b.Scores having b.Scores>80
–39、求每門課程的學生人數 select a.CID[課程編號],a.Cname[課程名稱] ,count(*)[學生人數] from Course a,Score b where a.CID=b.CID group by a.CID,a.Cname
–40、查詢選修”秦紅山”老師所授課程的學生中,成績最高的學生資訊及其成績 select top 1 a.Sname [姓名],b.Scores[分數] from Student a,Score b,Course c ,Teacher d where a.SID=b.SID and b.CID=c.CID and c.TID=d.TID and d.Tname=’秦紅山’ group by a.Sname,b.Scores order by b.Scores desc
–41、查詢不同課程成績相同的學生的學生編號、課程編號、學生成績
–42、查詢每門功成績最好的前兩名 select a.CID,a.Scores from Score a where a.Scores in (select top 2 Scores from Score where Score.CID=a.CID order by Scores desc ) order by a.Scores,a.CID desc
–44、檢索至少選修兩門課程的學生學號 select a.SID,a.Sname from Student a ,Score b where a.SID=b.SID group by a.SID,a.Sname having COUNT(b.CID)>=2
–45、查詢選修了全部課程的學生資訊