create table Student--3rd再次執行 ( Sno int primary key not null,--學號主鍵 Sname varchar(50) not null,--學生姓名 Ssex varchar(50) not null,--學生性別 Sbirthday datetime,--出生年月 Class int--班級 ) truncate table Student--清空表格 insert into Student values(108,'曾華','男','1977-09-01',95033) insert into Student 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) select *from Student delete from Student create table Course--2nd其次執行 ( Cno char(5) primary key not null,--課程號主鍵 Cname Varchar(10)not null,--課程名稱 Tno Varchar(50) not null--教工編號(外碼) foreign key(Tno) references Teacher (Tno), ) 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') select *from Course delete from Course create table Score--4th最后執行 ( Sno int not null, --學號(外碼) Cno Char(5) not null,--課程號(外碼) Degree Decimal(4,1),--成績 foreign key(Sno) references Student(Sno),--學號(外碼) foreign key(Cno) references Course(Cno),--課程號(外碼) primary key(Sno,Cno) ) 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) delete from Score create table Teacher--1st首先執行 ( Tno varchar(50) primary key not null,--教工編號(主碼) Tname varchar(50)not null,--教工姓名 Tsex varchar(50)not null,--教工性別 Tbirthday datetime,--教工出生年月 Prof varchar(50),--職稱 Depart Varchar(10)not null--教工所在部門 ) 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','助教','電子工程系') select *from Student select *from Course select *from Teacher select *from Score delete from Teacher --1.查詢Student表中的所有記錄的Sname、Ssex和Class列。 select Sname,Ssex,Class from Student; --2.查詢教師所有的單位即不重復的Depart列。 select distinct Depart from Teacher --3.查詢Student表的所有記錄。 select * from Student --4.查詢Score表中成績在60到80之間的所有記錄。 select *from Score where Degree between 60 and 80; --5.查詢Score表中成績為85,86或88的記錄。 select *from Score where Degree in(85,86,88); --6.查詢Student表中“95031”班或性別為“女”的同學記錄。 select *from Student where Class=95031 or Ssex='女'; --7.以Class降序查詢Student表的所有記錄。 select *from Student order by Class desc; --8.以Cno升序、Degree降序查詢Score表的所有記錄。 select *from Score order by Cno,Degree desc; --9.查詢“95031”班的學生人數。--聚合函數,針對數據列,計算求和或者計數等一系列算數性,返回結果是一個數值 --SUM(數值類型列) AVG(數值類型列) MAX(數值類型列) MIN(數值類型列) COUNT(*) select COUNT(*) from Student where Class=95031 select MAX(Degree) as maxfen ,MIN(Degree) minfen from Score where Cno='3-105'--as起別名 --10.查詢Score表中的最高分的學生學號和課程號。(子查詢或者排序) select Sno,Cno from Score where Degree=(select MAX(Degree) from Score); select top 1 *from Score order by Degree desc--從大到校排序再取第一行 --11.查詢‘3-105’號課程的平均分。 Select AVG(Degree) from Score where Cno='3-105'; --查詢每門課的平均成績--分組和聚合結合的時候,先分組,然后對每一組分別進行聚合 Select AVG(Degree) from Score group by Cno;--先按Cno分組,在對Cno聚合 --12.查詢Score表中至少有5名學生選修的並以3開頭的課程的平均分數。 select x.Cno ,avg(x.Degree) from Score x where Cno like '3%' and 5<(select count (*) from Score y where x.Cno = y.Cno) group by x.Cno select Cno,AVG(Degree) from Score where Cno like '3%' group by Cno having COUNT(*)>=5--篩選分組完了再對每一組進行AVG聚合,分完組之后相當於只剩下分完組的列 select Cno,AVG(Degree) as degreess from Score where Cno like '3%' group by Cno having COUNT(*)>=1 order by degreess--order by理解為最后才執行 --13.查詢最低分大於70,最高分小於90的Sno列。 select x.Sno from Score x where 70 < (select MIN(y.Degree) from Score y where x.Sno=y.Sno)and 90>(select max(z.Degree) from Score z where x.Sno=z.Sno); select Sno from Score where Degree >70 and Degree <90--分數大於70,小於90的Sno列 --14.查詢所有學生的Sname、Cno和Degree列。--表連接,一定有主外鍵關系:都好隔開兩個表形成笛卡爾積,再進行where篩選,通過表主外鍵關系篩選,where只是個篩選條件 select Sname, Cno, Degree from Student,Score where Student.Sno = Score.Sno; --15.查詢所有學生的Sno、Cname和Degree列。--left right select Sno,Cname,Degree from Score inner join course on Score.Cno=Course.Cno--不寫默認也是有inner select Student.Sno,Sname,Ssex,Student.Class,Score.Sno,Score.Cno,Score.Degree from Student left join Score on Student.Sno=Score.Sno--left join左表顯示全,右表有關系的顯示,沒有的顯示為空 select Student.Sno,Sname,Ssex,Student.Class,Score.Sno,Score.Cno,Score.Degree from Student right join Score on Student.Sno=Score.Sno select Sno,(select Cname from Course where Score.Cno=Course.Cno) as 課程 ,Degree from Score--Cname不在Score表中,用子查詢時()內的Cname必訓as一個新名稱 --16.查詢所有學生的Sname、Cname和Degree列。 select Sname,Cname,Degree from student join Score on Student.Sno=Score.Sno join Course on Score.Cno=Course.Cno --17.查詢“95033”班所選課程的平均分。 select AVG(degree)from Student join Score on Student.Sno=Score.Sno and Class='95033' --18.★假設使用如下命令建立了一個grade表: create table grade(low int,upp int,[rank] char(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') select *from grade --現查詢所有同學的Sno、Cno和rank列。(三種方法) select Sno,Cno,Degree,[rank] from Score,grade where Degree between low and upp order by [rank] select Sno,Cno,Degree,[rank] from Score join grade on Degree between low and upp order by [rank]--on后面加篩選條件 select Sno,Cno,(select [rank] from grade where Score.Degree between low and upp) as LV from Score order by Degree desc--子查詢得到的結果必須唯一 --19.查詢選修“3-105”課程的成績高於“109”號同學成績的所有同學的記錄。 select*from Score where Cno='3-105'and degree>(select degree from score where Sno='109' and Cno='3-105') --20.查詢score中選學多門課程的同學中分數為非最高分成績的記錄。select x.* from Score x where Degree<(select MAX(y.Degree) from Score y where y.Sno=x.Sno) select * from score as a where sno in(select sno from score group by sno having COUNT(*)>1 )and degree <(select MAX(degree) from score as b where b.cno = a.cno group by cno)--去掉每門課的最高分 select *from Score a where Sno in(select Sno from Score group by Sno having COUNT(*)>1) and Degree not in (select MAX(Degree) from Score b group by Cno having b.Cno=a.Cno)--去掉每門課的最高分 select *from Score where Sno in(select Sno from Score group by Sno having COUNT(*)>1) and Degree not in(select MAX(Degree) from Score where Sno in(select Sno from Score group by Sno having COUNT(*)>1))--除去全部課程中的最高分 select *from Score a where Degree not in (select MAX(Degree) from Score b group by Cno having b.Cno=a.Cno)--查詢除了每門課最高分之外的其他學生分數信息 --a.Con=b.Con是比較同一門課程(Con)的條件下篩選的,相當於在課程名稱相同的情況下比較分數,除去每一門課程的最高分 1.查詢學號出現兩次及以上的學號出來 select Sno from Score group by Sno having COUNT(*)>1 2.查詢步驟1中的學號的人的分數 select *from Score where Sno in(select Sno from Score group by Sno having COUNT(*)>1) 3.查詢所有人的分數最高分 select MAX(Degree) from Score where Sno in(select Sno from Score group by Sno having COUNT(*)>1) 4.從步驟2的結果中提出最高分即步驟3的 select *from Score where Sno in(select Sno from Score group by Sno having COUNT(*)>1)and Degree!=(select MAX(Degree) from Score) --21.查詢成績高於學號為“109”、課程號為“3-105”的成績的所有記錄。 select *from score where Degree>(select degree from score where sno='109'and Cno='3-105') --22.查詢和學號為108的同學同年出生的所有學生的Sno、Sname和Sbirthday列。 select sno,Sname,sbirthday from student where YEAR(Sbirthday)=(select YEAR(Sbirthday) from Student where Sno='109') --23.查詢“張旭“教師任課的學生成績。 select degree from Score where Cno in (select Cno from Course where Tno in (select Tno from Teacher where Tname = '張旭')) select degree from Score join Course on Score.Cno=Course.Cno join Teacher on Course.Tno=Teacher.Tno 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)) --25.查詢95033班和95031班全體學生的記錄。 select*from student inner join Score on Student.Sno=Score.Sno where Class in(95033,95031) select *from Student,Score where Class in(95033,95031) and Student.Sno=Score.Sno --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='計算機系')) select Sno,Score.Cno,Degree from Score join Course on Score.Cno=Course.Cno join Teacher on Course.Tno=Teacher.Tno where Depart='計算機系' --28、查詢“計算機系”與“電子工程系“不同職稱的教師的Tname和Prof。 select Tname,Prof from Teacher where Prof not in (select prof from Teacher where Depart='電子工程系' and Prof in (select Prof from Teacher where Depart='計算機系')) and Depart in ('計算機系','電子工程系') --查詢兩個系中教師相同職稱名稱,不在這里面的就是除去兩個系都有的剩下的,見上面 select prof from Teacher where Depart='電子工程系' and Prof in (select Prof from Teacher where Depart='計算機系') --29、查詢選修編號為“3-105“課程且成績至少高於選修編號為“3-245”的同學的Cno、Sno和Degree,並按Degree從高到低次序排序。 select*from Score where Cno='3-105'and Degree>(select MAX(Degree) from Score where Cno='3-245') order by Degree --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.--表連接,union(縱向連接,表1和表2數據類型要對應且列數對應)上下鏈接,與join on左右相關鏈接不同 select sname,ssex,sbirthday from student union select Tname,Tsex,Tbirthday from Teacher --32.查詢所有“女”教師和“女”同學的name、sex和birthday. select sname,ssex,sbirthday from student where Ssex='女' union select Tname,Tsex,Tbirthday from Teacher where Tsex='女' --33.查詢成績比該課程平均成績低的同學的成績表。--相關子查詢--同一門學科的平均分,,每門學科低於自身平均分的 select * from score as a where a.degree< (select AVG(degree) from score as b where a.cno = b.cno group by cno)--相當於foreach列出每一門課的平均分 select * from score as a where a.degree< (select AVG(degree) from score as b group by cno having a.cno = b.cno) --34.查詢所有任課教師的Tname和Depart. select Tname,Depart from Teacher where Tno in(select distinct Tno from Course) --35.查詢所有未講課的教師的Tname和Depart. select Tname,Depart from Teacher where Tno in(select Tno from Course where Cno not in(select Cno from Score group by Cno)) select Tname,Depart from Teacher where Tno not in(select Tno from Course) --36.查詢至少有2名男生的班號。 select Class from Student where Ssex='男' group by Class having COUNT(*)>=2 --37.查詢Student表中不姓“王”的同學記錄。 select *from Student where Sname not like '王%' --38.★查詢Student表中每個學生的姓名和年齡。 select Sname,nianling=(select DATEDIFF(YEAR,Sbirthday,GETDATE())) from Student --39.★查詢Student表中最大和最小的Sbirthday日期值。 select max(Sbirthday)as [max],MIN(Sbirthday) as[min] from Student --40、以班號和年齡從大到小的順序查詢Student表中的全部記錄。 select *from Student order by Class desc,Sbirthday asc --41、查詢“男”教師及其所上的課程。 select Teacher.Tno,Tname,Tsex,Cname,Cno from Teacher join Course on Teacher.Tno=Course.Tno where Tsex='男' --42、查詢最高分同學的Sno、Cno和Degree列。 select *from Score where Degree=(select MAX(Degree)from Score) --43、查詢和“李軍”同性別的所有同學的Sname. select Sname from Student where Ssex in (select Ssex from Student where Sname='李軍') --44、查詢和“李軍”同性別並同班的同學Sname. select Sname from Student where Ssex=(select Ssex from Student where Sname='李軍') and Class iN (select Class from Student where Sname='李軍') --45、查詢所有選修“計算機導論”課程的“男”同學的成績表。 select *from Score where Sno in(select Sno from Student where Ssex='男') and Cno=(select Cno from Course where Cname='計算機導論');
--至查詢student表的第3、4行--分頁查詢 ★必須要用主鍵Sno來排除,防止重復 select *from Student select top 2 *from Student where Sno not in(select top 2 Sno from Student)--第二頁,屏蔽掉當前頁的前面頁的內容,前面top是取屏蔽之后的數據的一頁顯示條數 --2行為一頁 select top 2 *from Student where Sno not in(select top 4 Sno from Student)--第三頁