一个项目涉及到的50个Sql语句Word文档格式.docx
《一个项目涉及到的50个Sql语句Word文档格式.docx》由会员分享,可在线阅读,更多相关《一个项目涉及到的50个Sql语句Word文档格式.docx(31页珍藏版)》请在冰点文库上搜索。
![一个项目涉及到的50个Sql语句Word文档格式.docx](https://file1.bingdoc.com/fileroot1/2023-5/2/7003cd54-8a46-4f66-97ab-8a3f7742748d/7003cd54-8a46-4f66-97ab-8a3f7742748d1.gif)
1990-12-21'
03'
孙风'
1990-05-20'
04'
李云'
1990-08-06'
05'
周梅'
1991-12-01'
女'
06'
吴兰'
1992-03-01'
07'
郑竹'
1989-07-01'
08'
王菊'
1990-01-20'
createtableCourse(C#varchar(10),Cnamenvarchar(10),T#varchar(10))
insertintoCoursevalues('
语文'
数学'
英语'
createtableTeacher(T#varchar(10),Tnamenvarchar(10))
insertintoTeachervalues('
张三'
李四'
王五'
createtableSC(S#varchar(10),C#varchar(10),scoredecimal(18,1))
insertintoSCvalues('
80)
90)
99)
70)
60)
50)
30)
20)
76)
87)
31)
34)
89)
98)
go
--1、查询"
01"
课程比"
02"
课程成绩高的学生的信息及课程分数
--1.1、查询同时存在"
课程和"
课程的情况
selecta.*,b.score[课程'
的分数],c.score[课程'
的分数]fromStudenta,SCb,SCc
wherea.S#=b.S#anda.S#=c.S#andb.C#='
andc.C#='
andb.score>
c.score
结果:
S#SnameSageSsex课程'
的分数课程'
的分数
02钱电1990-12-2100:
00:
00.000男70.060.0
04李云1990-08-0600:
00.000男50.030.0
--1.2、查询同时存在"
课程的情况和存在"
课程但可能不存在"
课程的情况(不存在时显示为null)(以下存在相同内容时不再解释)
selecta.*,b.score[课程"
的分数],c.score[课程"
的分数]fromStudenta
leftjoinSCbona.S#=b.S#andb.C#='
leftjoinSCcona.S#=c.S#andc.C#='
whereb.score>
isnull(c.score,0)
S#SnameSageSsex课程"
的分数课程"
06吴兰1992-03-0100:
00.000女31.0NULL
--2、查询"
课程成绩低的学生的信息及课程分数
--2.1、查询同时存在"
andb.score<
01赵雷1990-01-0100:
00.000男80.090.0
05周梅1991-12-0100:
00.000女76.087.0
--2.2、查询同时存在"
课程的情况和不存在"
课程但存在"
whereisnull(b.score,0)<
07郑竹1989-07-0100:
00.000女NULL89.0
--3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
selecta.S#,a.Sname,cast(avg(b.score)asdecimal(18,2))avg_score
fromStudenta,scb
wherea.S#=b.S#
groupbya.S#,a.Sname
havingcast(avg(b.score)asdecimal(18,2))>
=60
orderbya.S#
--cast()用于数据类型转换。
--如cast('
123'
asint)--把字符123转为int型
S#Snameavg_score
01赵雷89.67
02钱电70.00
03孙风80.00
05周梅81.50
07郑竹93.50
--4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
--4.1、查询在sc表存在成绩的学生信息的SQL语句。
havingcast(avg(b.score)asdecimal(18,2))<
60
04李云33.33
06吴兰32.50
--4.2、查询在sc表中不存在成绩的学生信息的SQL语句。
selecta.S#,a.Sname,isnull(cast(avg(b.score)asdecimal(18,2)),0)avg_score
fromStudentaleftjoinscb
ona.S#=b.S#
havingisnull(cast(avg(b.score)asdecimal(18,2)),0)<
08王菊0.00
--5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
--5.1、查询所有有成绩的SQL。
selecta.S#[学生编号],a.Sname[学生姓名],count(b.C#)选课总数,sum(score)[所有课程的总成绩]
fromStudenta,SCb
wherea.S#=b.S#
groupbya.S#,a.Sname
学生编号学生姓名选课总数所有课程的总成绩
01赵雷3269.0
02钱电3210.0
03孙风3240.0
04李云3100.0
05周梅2163.0
06吴兰265.0
07郑竹2187.0
--5.2、查询所有(包括有成绩和无成绩)的SQL。
fromStudentaleftjoinSCb
ona.S#=b.S#
08王菊0NULL
--6、查询"
李"
姓老师的数量
--方法1
selectcount(Tname)["
姓老师的数量]fromTeacherwhereTnamelikeN'
李%'
--方法2
姓老师的数量]fromTeacherwhereleft(Tname,1)=N'
李'
"
姓老师的数量
1
--7、查询学过"
张三"
老师授课的同学的信息
selectdistinctStudent.*fromStudent,SC,Course,Teacher
whereStudent.S#=SC.S#andSC.C#=Course.C#andCourse.T#=Teacher.T#andTeacher.Tname=N'
orderbyStudent.S#
S#SnameSageSsex
00.000男
03孙风1990-05-2000:
00.000女
--8、查询没学过"
selectm.*fromStudentmwhereS#notin(selectdistinctSC.S#fromSC,Course,TeacherwhereSC.C#=Course.C#andCourse.T#=Teacher.T#andTeacher.Tname=N'
)orderbym.S#
08王菊1990-01-2000:
--9、查询学过编号为"
并且也学过编号为"
的课程的同学的信息
selectStudent.*fromStudent,SCwhereStudent.S#=SC.S#andSC.C#='
andexists(Select1fromSCSC_2whereSC_2.S#=SC.S#andSC_2.C#='
)orderbyStudent.S#
--方法3
selectm.*fromStudentmwhereS#in
(
selectS#from
(
selectdistinctS#fromSCwhereC#='
unionall
)tgroupbyS#havingcount
(1)=2
orderbym.S#
--10、查询学过编号为"
但是没有学过编号为"
andnotexists(Select1fromSCSC_2whereSC_2.S#=SC.S#andSC_2.C#='
andStudent.S#notin(SelectSC_2.S#fromSCSC_2whereSC_2.S#=SC.S#andSC_2.C#='
--11、查询没有学全所有课程的同学的信息
--11.1、
selectStudent.*
fromStudent,SC
whereStudent.S#=SC.S#
groupbyStudent.S#,Student.Sname,Student.Sage,Student.Ssexhavingcount(C#)<
(selectcount(C#)fromCourse)
--11.2
fromStudentleftjoinSC
onStudent.S#=SC.S#
--12、查询至少有一门课与学号为"
的同学所学相同的同学的信息
selectdistinctStudent.*fromStudent,SCwhereStudent.S#=SC.S#andSC.C#in(selectC#fromSCwhereS#='
)andStudent.S#<
>
'
--13、查询和"
号的同学学习的课程完全相同的其他同学的信息
selectStudent.*fromStudentwhereS#in
(selectdistinctSC.S#fromSCwhereS#<
andSC.C#in(selectdistinctC#fromSCwhereS#='
)
groupbySC.S#havingcount
(1)=(selectcount
(1)fromSCwhereS#='
))
--14、查询没学过"
老师讲授的任一门课程的学生姓名
selectstudent.*fromstudentwherestudent.S#notin
(selectdistinctsc.S#fromsc,course,teacherwheresc.C#=course.C#andcourse.T#=teacher.T#andteacher.tname=N'
orderbystudent.S#
--15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
selectstudent.S#,student.sname,cast(avg(score)asdecimal(18,2))avg_scorefromstudent,sc
wherestudent.S#=SC.S#andstudent.S#in(selectS#fromSCwherescore<
60groupbyS#havingcount
(1)>
=2)
groupbystudent.S#,student.sname
--16、检索"
课程分数小于60,按分数降序排列的学生信息
selectstudent.*,sc.C#,sc.scorefromstudent,sc
wherestudent.S#=SC.S#andsc.score<
60andsc.C#='
orderbysc.scoredesc
--17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
--17.1SQL2000静态
selecta.S#学生编号,a.Sname学生姓名,
max(casec.CnamewhenN'
thenb.scoreelsenullend)[语文],
thenb.scoreelsenullend)[数学],
thenb.scoreelsenullend)[英语],
cast(avg(b.score)asdecimal(18,2))平均分
fromStudenta
leftjoinSCbona.S#=b.S#
leftjoinCourseconb.C#=c.C#
orderby平均分desc
--17.2SQL2000动态
declare@sqlnvarchar(4000)
set@sql='
selecta.S#'
+N'
学生编号'
+'
a.Sname'
学生姓名'
select@sql=@sql+