分享一些常見的SQL語句,包含單表查詢、高級查詢(連接查詢、復合條件查詢、嵌套查詢等)。
--建立學生信息表Students create table Students ( SId char(5) not null primary key, SName nvarchar(20) unique, SGender char(10) default('Male'), SAge int, SSdept nvarchar(250) ) --課程表 create table Course ( CNo Char(4) not null primary key, CName nvarchar(50), CPNo char(4), CCreadit smallint foreign key(cpno) references course(cno) ) --學生選課表 create table StudentCourse ( SCId char(5) not null , SCCNo char(4) not null, SCGrade smallint, primary key(Scid,sccno), foreign key(SCId) references Students(Sid), foreign key(sccno) references Course(cno) ) --查詢每個系的學生人數 select COUNT(*) as '人數',SSdept as '所在系' from Students group by SSdept --查詢計算機系男女生人數 select COUNT(*) as '人數',SGender 性別 from Students where ssdept='計算機科學與技術' group by SGender --查詢每個系男女生人數 select COUNT(*) as '人數',SSdept as '系',SGender as '性別' from students group by ssdept,SGender --查詢男生人數超過1的系 select ssdept as '所在系',COUNT(*) as '人數' from Students where SGender='Male' group by ssdept having COUNT(*)>2 --查詢和張三同一系的學生學號、姓名 select SId as '學號' ,SName as '姓名' from Students where SSdept=(select ssdept from Students where SName='張三') and SName<>'張三' --查詢比張三年紀大的學生的姓名、性別 select SName as '姓名',SGender as '性別' from Students where SAge>(select sage from students where sname='張三') --查詢張三的學號和其選修的課程號和成績 select sc.SCCNo as '課程號',sc.SCGrade as '成績' from students as s,Course c,StudentCourse sc where s.SId=sc.SCId and sc.SCCNo=c.CNo and s.SName='張三' --查詢張三選修高等數學上這門課的成績 select sc.SCCNo as '課程號',c.CName as '課程名',sc.SCGrade as '成績' from students as s,Course c,StudentCourse sc where s.SId=sc.SCId and sc.SCCNo=c.CNo and c.CName='高等數學上' and s.SName='張三' --查詢與張三一樣大的學生姓名,性別,年齡。 select SName as '姓名',SGender as '性別',SAge as '年齡' from Students where SAge=(select SAge from Students where SName='張三') and SName<>'張三' --查詢選修了高等數學上的學生的學號、姓名 select s.SId as '學號',s.SName as '姓名' from students as s,Course c,StudentCourse sc where s.SId=sc.SCId and sc.SCCNo=c.CNo and c.CName='高等數學上' --查詢張三選修的所有課程號、課程名稱、成績 select sc.SCCNo as '課程號',c.CName as '課程名',sc.SCGrade as '成績' from students as s,Course c,StudentCourse sc where s.SId=sc.SCId and sc.SCCNo=c.CNo and s.SName='張三' --查詢學習了張三選修某門課程的學生學號、姓名、年齡 select * from Students s1,StudentCourse sc1 where sc1.SCCNo in ( select sc.SCCNo from Students s,StudentCourse sc where sc.SCId=s.SId and s.SName='張三' ) and sc1.SCId=s1.SId --查詢張三選修的所有課程的課程號、課程名稱 select sc.SCCNo as '課程號',c.CName as '課程名' from Students s,Course c,StudentCourse sc where s.SId=sc.SCId and sc.SCCNo=c.CNo and s.SName='張三' --查詢比張三年齡大的學生學號、姓名 select SId as '學號',SName as '姓名' from Students where SAge>(select SAge from Students where SName='張三') --查詢選修每門課程中成績小於其平均成績的學生學號 select sc1.SCId as '學生學號' from StudentCourse sc1 where SCGrade< ( select AVG(SCGrade) from StudentCourse sc2 where sc2.SCCNo=sc1.SCCNo ) --查詢張三選修高等數學上的課程的成績 select * from StudentCourse sc ,Students s,Course c where sc.SCId=s.SId and sc.SCCNo=c.CNo and s.SName='張三' and c.CName='高等數學上' select * from StudentCourse where SCCNo= ( Select Cno from Course where CName='高等數學上' ) and SCId= ( select sid from Students where SName='張三' ) --查詢張三選修課程的平均成績 select AVG(sc.SCGrade) as '平均成績' from StudentCourse sc ,Students s where sc.SCId=s.SId and s.SName='張三' --查詢選修課程的平均成績小於張三平均成績的學生學號 select sC.SCId from StudentCourse sc group by sc.SCId having AVG(SC.SCGrade)< ( select AVG(sc.SCGrade) from StudentCourse sc ,Students s where sc.SCId=s.SId and s.SName='張三' ) --查詢課程的平均成績低於張三平均成績的課程號 select sC.SCCNo from StudentCourse sc group by sc.SCCNo having AVG(SC.SCGrade)< ( select AVG(sc.SCGrade) from StudentCourse sc ,Students s where sc.SCId=s.SId and s.SName='張三' ) --查詢選修課程成績大於等於該課程平均成績的學生學號 select SCId from StudentCourse sc1 where sc1.SCGrade >= ( select avg( sc2.SCGrade ) from StudentCourse sc2 where sc2.SCCNo=sc1.SCCNo )