create database School use School go create table Student --1.學生表 ( Sno varchar(20) not null primary key,--學號(主碼) Sname varchar(20) not null,--學生姓名 Ssex varchar(20) not null,--學生性別 Sbirthday datetime,--學生出生年月 Class varchar(20),--學生所在班級 ) go create table Teacher --4.教師表 ( Tno varchar(20) not null primary key,--教工編號(主) Tname varchar(20) not null,--教工姓名 Tsex varchar(20) not null,--教工性別 Tbirthday datetime,--教工出生年月 Prof varchar(20),--職稱 Depart varchar(20) not null,--教工所在部門 ) go create table Course --2.課程表 ( Cno varchar(20) not null primary key,--課程號(主) Cname varchar(20) not null,--課程名稱 Tno varchar(20) not null references Teacher(Tno),--教工編號(外) ) go create table Score --3.成績表 ( Sno varchar(20) not null references Student(Sno),--學號(外) Cno varchar(20) not null references Course(Cno),--課程號(外) Degree decimal(4,1),--成績 ) alter table Score add constraint Pk_Score primary key (Sno,Cno) --添加兩個主鍵 --學生信息 insert into Student values('108','曾華','男','1977-09-01','95033') insert into Student(Sno,Sname,Ssex,Sbirthday,Class)values('105','匡明','男','1975-10-02','95031') insert into Student values('107','王麗','女','1976-01-23','95033') insert into Student values('101','李軍','男','1976-02-20','95033') insert into Student values('109','王芳','女','1975-02-10','95031') insert into Student values('103','陸君','男','1974-06-03','95031') --職工信息 insert into Teacher values('804','李誠','男','1958-12-02','副教授','計算機系') insert into Teacher values('856','張旭','男','1969-03-12','講師','電子工程系') insert into Teacher values('825','王萍','女','1972-05-05','助教','計算機系') insert into Teacher values('831','劉冰','女','1977-08-14','助教','電子工程系') --課程 insert into Course values('3-105','計算機導論','825') insert into Course values('3-245','操作系統','804') insert into Course values('6-166','數字電路','856') insert into Course values('9-888','高等數學','831') --成績 insert into Score values('103','3-245','86') insert into Score values('105','3-245','75') insert into Score values('109','3-245','68') insert into Score values('103','3-105','92') insert into Score values('105','3-105','88') insert into Score values('109','3-105','76') insert into Score values('101','3-105','64') insert into Score values('107','3-105','91') insert into Score values('108','3-105','78') insert into Score values('101','6-166','85') insert into Score values('107','6-166','79') insert into Score values('108','6-166','81') --1、 查詢Student表中的所有記錄的Sname、Ssex和Class列。 select Sname,Ssex,Class from Student --2、 查詢教師所有的單位即不重復的Depart列。 --去重 distinct select distinct Depart from Teacher --3、 查詢Student表的所有記錄。 select * from Student --4、 查詢Score表中成績在60到80之間的所有記錄。 --范圍查詢 比較運算符 select * from Score where Degree>=60 and Degree<=80 --between...and... select * from Score where Degree between 60 and 80 --5、 查詢Score表中成績為85,86或88的記錄。 select * from Score where Degree=85 or Degree=86 or Degree=88 --離散查詢 select * from Score where Degree in (85,86,88) --6、 查詢Student表中“95031”班或性別為“女”的同學記錄。 select * from Student where Class='95031' or Ssex='女' --7、 以Class降序查詢Student表的所有記錄。 --排序order by 升序asc 降序desc select * from Student order by Class desc --8、 以Cno升序、Degree降序查詢Score表的所有記錄。 select * from Score order by Cno asc,Degree desc --9、 查詢“95031”班的學生人數。 --個數count(*) select Class,COUNT(*) from Student group by Class having Class=95031 select count(*) from student where class='95031' --10、 查詢Score表中的最高分的學生學號和課程號。(子查詢或者排序) --子查詢 select Sno,Cno from Score where Degree=(select max(Degree) from Score) --降序取第一條數據 select top 1 Sno,Cno from Score order by Degree desc --11、 查詢每門課的平均成績。 --平均分用avg(列) select Cno,AVG(Degree) as '平均分' from Score group by Cno --12、查詢Score表中至少有5名學生選修的並以3開頭的課程的平均分數。 --模糊查詢 like 分組 group by...having... select Cno,AVG(Degree) as '平均分' from Score group by Cno having COUNT(Cno)>=5 and Cno like'3%' select avg(degree) from score where cno like'3%' group by cno having count(*)>=5 --13、查詢分數大於70,小於90的Sno列。 --范圍查詢 select Sno from Score where Degree>70 and Degree<90 select Sno from Score where Degree between 70 and 90 --14、查詢所有學生的Sname、Cno和Degree列。 --連接查詢 select Student.Sname,Cno,Degree from Student,Score where Score.Sno=Student.Sno --join...on... select Sname,Cno,Degree from Student join Score on Student.Sno=Score.Sno --15、查詢所有學生的Sno、Cname和Degree列。 select Sno,Cname,Degree from Course,Score where Score.Cno=Course.Cno select Sno,Cname,Degree from Score join Course on Score.Cno=Course.Cno --16、查詢所有學生的Sname、Cname和Degree列。 select Sname,Cname,Degree from Student,Course,Score where Student.Sno=Score.Sno and Course.Cno=Score.Cno --17、 查詢“95033”班學生的平均分。 select Class='95033',AVG(degree) as '平均分' from Score join Student on Score.Sno=Student.Sno and Class='95033' select avg(degree) from score where sno in(select sno from student where class='95033') --18、 假設使用如下命令建立了一個grade表: create table grade ( low int, upp int, rank varchar(1) ) insert into grade values(90,100,'A') insert into grade values(80,89,'B') insert into grade values(70,79,'C') insert into grade values(60,69,'D') insert into grade values(0,59,'E') --現查詢所有同學的Sno、Cno和rank列。 select Sno,Cno,Degree,rank from grade join Score on Score.Degree between low and upp select sno,cno,rank from score,grade where degree between low and upp --19、查詢選修“3-105”課程的成績高於“109”號同學成績的所有同學的記錄。 select * from Score where Degree>(select Degree from Score where Cno='3-105' and Sno='109') and Cno='3-105' select * from Student join Score on Student.Sno=Score.Sno where Cno='3-105' and Degree>(select Degree from Score where Cno='3-105' and Sno='109') --20、查詢score中選學多門課程的同學中分數為非最高分成績的記錄。 --反向查詢沒有出現過得值not in select * from score a where sno in (select sno from score group by sno having count(*)>1) and degree <(select max(degree) from score b where b.cno=a.cno) select * from Score where Sno in(select Sno from Score group by Sno having COUNT(Sno)>=2 )and Degree not in( select max(Degree) from Score group by Cno) --21、查詢成績高於學號為“109”、課程號為“3-105”的成績的所有記錄。 select * from Score where Degree>(select Max(Degree) from Score where Sno='109')and Cno='3-105' --22、查詢和學號為108的同學同年出生的所有學生的Sno、Sname和Sbirthday列。 --獲取年year(時間日期) select Sno,Sname,Sbirthday from Student where YEAR(Sbirthday)=(select year(Sbirthday) from Student where Sno='108') select Sno,Sname,Sbirthday from Student where year(Sbirthday)=(select year(Sbirthday) from Student where Sno='107') --23、查詢“張旭“教師任課的學生成績。 select * from Score where Cno=(select Cno from Course where Tno=(select Tno from Teacher where Tname='張旭')) --24、查詢選修某課程的同學人數多於5人的教師姓名。 select Tname from Teacher where Tno=(select Tno from Course where Cno=(select Cno from Score group by Cno having COUNT(Cno)>5)) select Teacher.Tno,Tname,Cno,Cname from Teacher join Course on Teacher.Tno=Course.Tno where Tname in (select Tname from Teacher where Tno in (select Tno from Course where Cno in (select Cno from Score group by Cno having count(Cno)>=5))) --25、查詢95033班和95031班全體學生的記錄。 --與 or select * from Student where Class='95033' or Class='95031' --26、查詢存在有85分以上成績的課程Cno. select distinct Cno from Score where Degree>85 --27、查詢出“計算機系“教師所教課程的成績表。 select * from Score where Cno in (select Cno from Course where Tno in (select Tno from Teacher where Depart='計算機系')) --28、查詢“計算機系”與“電子工程系“depart不同職稱prof的教師的Tname和Prof。 --聯合查詢 select tname,prof from teacher where depart='計算機系' and prof not in(select prof from teacher where depart='電子工程系') union select tname,prof from teacher where depart='電子工程系' and prof not in(select prof from teacher where depart='計算機系') --相關子查詢 select Tname,prof from Teacher a where Prof not in (select Prof from Teacher b where a.Depart!=b.Depart) --29、查詢選修編號為“3-105“課程且成績至少高於選修編號為“3-245”的同學的Cno、Sno和Degree,並按Degree從高到低次序排序。 select Cno,Sno,Degree from Score where Cno='3-105' and Degree >(select MAX(Degree) from Score where Cno='3-245') order by Degree desc --any 列表中任意一個相當於max select * from score where cno='3-105' and degree>any(select degree from score where cno='3-245') --30、查詢選修編號為“3-105”且成績高於選修編號為“3-245”課程的同學的Cno、Sno和Degree. select * from Score where Cno='3-105' and Degree >(select MAX(Degree) from Score where Cno='3-245') --31、查詢所有教師和同學的name、sex和birthday. --聯合查詢 select Tname as name,Tsex as sex,Tbirthday as birthday from Teacher union select Sname,Ssex,Sbirthday from Student --32、查詢所有“女”教師和“女”同學的name、sex和birthday. --聯合查詢 條件查詢 select Tname as name,Tsex as sex,Tbirthday as birthday from Teacher where Tsex='女' union select Sname,Ssex,Sbirthday from Student where Ssex='女' --33、查詢成績比該課程平均成績低的同學的成績表。 select * from Score a where Degree <(select AVG(Degree) from Score b where a.Cno=b.Cno) --34、查詢所有任課教師的Tname和Depart. select Tname,Depart from Teacher --35、查詢所有未講課的教師的Tname和Depart. select Tname,Depart from Teacher where Tno not in (select Tno from Course where Cno in (select distinct Cno from Score)) --36、查詢至少有2名男生的班號。 select Class from Student where Ssex='男' group by Class having COUNT(*)>1 --37、查詢Student表中不姓“王”的同學記錄。 --不像not like select * from Student where Sname not like'王%' select * from Student where Sname not in (select Sname from Student where Sname like '王%') --38、查詢Student表中每個學生的姓名和年齡。 --當前系統時間getdate(),獲取數據庫時間sysdatetime() select Sname as '姓名',YEAR(GETDATE())-YEAR(Sbirthday) as '年齡' from Student --39、查詢Student表中最大和最小的Sbirthday日期值。 --最大值max(列)最小值min(列) select MAX(Sbirthday) as '大',MIN(Sbirthday) as '小' from Student --40、以班號和年齡從大到小的順序查詢Student表中的全部記錄。 --多個條件進行排序用,連接 select * from Student order by Class desc,Sbirthday desc --41、查詢“男”教師及其所上的課程。 select Teacher.Tno,Tname,Tsex,Cno,Cname from Teacher,Course where Teacher.Tno=Course.Tno and Tsex='男' --42、查詢最高分同學的Sno、Cno和Degree列。 select * from Score where Degree=(select MAX(Degree) from Score) --43、查詢和“李軍”同性別的所有同學的Sname. select Sname from Student where Ssex=(select Ssex from Student where Sname='李軍') --44、查詢和“李軍”同性別並同班的同學Sname. select Sname from Student where Ssex in (select Ssex from Student where Sname='李軍') and Class in (select Class from Student where Sname='李軍') --45、查詢所有選修“計算機導論”課程的“男”同學的成績表。 select * from Score where Cno in (select Cno from Course where Cname='計算機導論') and Sno in (select Sno from Student where Ssex='男')