语句写法.docx
- 文档编号:27477747
- 上传时间:2023-07-01
- 格式:DOCX
- 页数:11
- 大小:15.92KB
语句写法.docx
《语句写法.docx》由会员分享,可在线阅读,更多相关《语句写法.docx(11页珍藏版)》请在冰豆网上搜索。
语句写法
1、查询<学生信息表>,查询学生"张三"的全部基本信息
Select*
fromA_studentinfo
wheresname='张三'
2、查询<学生信息表>,查询学生"张三"和”李四”的基本信息
Select*
fromA_studentinfo
wheresname='张三'
orsname='李四'
3、查询<学生信息表>,查询姓"张"学生的基本信息
Select*
fromA_studentinfo
wheresnamelike'张%'
4、查询<学生信息表>,查询姓名中含有"四"字的学生的基本信息
Select*
fromA_studentinfo
wheresnamelike'%四%'
5、查询<学生信息表>,查询姓名长度为三个字,姓“李”,且最后一个字是“强”的全部学生信息。
select*
fromA_studentinfo
wheresnamelike'李_强'
6、查询<学生信息表>,查询姓"张"或者姓”李”的学生的基本信息。
Select*
fromA_studentinfo
wheresnamelike'张%'
orsnamelike'李%'
7、查询<学生信息表>,查询姓"张"并且"所属省份"是"北京"的学生信息
Select*
fromA_studentinfo
wheresnamelike'张%'
andprovince='北京'
8、查询<学生信息表>,查询"所属省份"是"北京"、”新疆”、”山东”或者"上海"的学生的信息
Select*
fromA_studentinfo
whereprovincein('北京','上海','新疆','山东')
9、查询<学生信息表>,查询姓"张",但是"所属省份"不是"北京"的学生信息
Select*
fromA_studentinfo
wheresnamelike'张%'
andprovince!
='北京'
10、查询<学生信息表>,查询全部学生信息,并按照“性别”排序,性别相同的情况下按照“所属省份”排序,所属省份相同的情况下再按照“班级”排序
select*
fromA_studentinfo
orderbysex,province,class
11、查询<学生信息表>,查询现有学生都来自于哪些不同的省份
selectdistinctprovinceas省份
fromA_studentinfo
12、查询<学生选修信息表>,查询没有填写成绩的学生的学号、课程号和成绩
Select*
fromA_studentcourse
wherescoreisnull
13、查询<学生选修信息表>,查询全部填写了成绩的学生的选修信息,并按照“成绩”从高到低进行排序
Select*
fromA_studentcourse
wherescoreisnotnull
orderbyscoredesc
二、聚合函数练习
1、统计<学生信息表>,统计共有多少个学生
Selectcount(*)as学生数量
fromA_studentinfo
2、统计<学生信息表>,统计年龄大于20岁的学生有多少个
Selectcount(*)as学生数量
fromA_studentinfo
where(2008-yearofbirth)>20
3、统计<学生信息表>,统计入学时间在1980年至1982年的学生人数
selectcount(*)as学生数量
fromA_studentinfo
whereenrollmentbetween'1998-01-01'and'2003-12-30'
对比以下查询方式,看看有何不同,为什么?
selectcount(*)as学生数量
fromA_studentinfo
whereenrollmentbetween'1998'and'2003'
4、统计<学生选修信息表>,统计学号为"S001"的学生的平均成绩
Selectavg(score)as平均成绩
fromA_studentcourse
wheresno='S001'
5、统计<学生选修信息表>,统计学号为"S001"的学生的总成绩
selectsum(score)as总成绩
fromA_studentcourse
wheresno='S001'
6、统计<学生选修信息表>,查询课程号为”C001”的课程的最高成绩
selectmax(score)as最高成绩
fromA_studentcourse
wherecno='C001'
7、统计<学生信息表>,查询所有学生中的最大年龄是多少
select2008-min(yearofbirth)as最大年龄
fromA_studentinfo
三、分组查询练习
1、统计<学生选修信息表>,统计每个课程的选修人数
selectcno,count(*)as学生数量
fromA_studentcourse
groupbycno
2、统计<学生选修信息表>,统计每个同学的总成绩
selectsno,sum(score)as总成绩
fromA_studentcourse
groupbysno
3、统计<学生信息表>,统计每个班级中每种性别的学生人数,并按照班级排序
selectclassas班级,sexas性别,count(*)as人数
fromA_studentinfo
groupbyclass,sex
orderbyclass
4、统计<学生选修信息表>,统计每门课程的平均成绩,并按照成绩降序排序
Selectcno,avg(score)as平均成绩
fromA_studentcourse
groupbycno
orderbyavg(score)desc
5、统计<学生选修信息表>,显示有两门以上课程不及格的学生的学号
Selectsnoas不及格学生学号
fromA_studentcourse
wherescore<60
groupbysno
havingcount(*)>1
6、统计<学生信息表>,统计每个班级中的最大年龄是多少
selectclassas班级,2008-min(yearofbirth)as最大年龄
fromA_studentinfo
groupbyclass
四、嵌套查询练习
1、用子查询实现,查询选修“高等数学”课的全部学生的总成绩
selectsum(score)as高等数学总成绩
fromA_studentcourse
wherecno=
(
selectcno
fromA_courseinfo
wheresubject='高等数学'
)
2、用子查询实现,统计<学生选修信息表>,显示学号为"S001"的学生在其各科成绩中,最高分成绩所对应的课程号和成绩
selectscore,cno
fromA_studentcourse
wheresno='S001'
andscore=
(
selectmax(score)
fromA_studentcourse
wheresno='S001'
)
思考:
如果该学号学生有两个课程分数都为最高的100分,查询会有什么结果
3、用子查询实现,查询2班选修"数据库技术"课的所有学生的成绩之和
selectsum(score)as数据库技术总成绩
fromA_studentcourse
wherecno=
(
selectcno
fromA_courseinfo
wheresubject='数据库技术')
andsnoin
(
selectsno
fromA_studentinfo
whereclass='2'
)
4、用子查询实现,查询3班"张三"同学的"测试管理"成绩
selectscore
fromA_studentcourse
wherecno=
(
selectcno
fromA_courseinfo
wheresubject='测试管理'
)
andsnoin
(
selectsno
fromA_studentinfo
whereclass='3'
andsname='张三'
)
五、联接查询练习
1、查询"张三"的各科考试成绩,要求显示姓名、课程号和成绩
selectsnameas姓名,cnoas课程号,scoreas成绩
fromA_studentinfo,A_studentcourse
whereA_studentinfo.sno=A_studentcourse.sno
andsname='张三'
2、查询"张三"的各科考试成绩中,哪科没有记录考试成绩,要求显示姓名、课程号和成绩
selectsnameas姓名,cnoas课程号,scoreas成绩
fromA_studentinfo,A_studentcourse
whereA_studentinfo.sno=A_studentcourse.sno
andsname='张三'
andscoreisnull
3、查询"张三"的各门课程成绩,要求显示姓名、课程名称和成绩
selectsnameas姓名,subjectas课程名称,scoreas成绩
fromA_studentinfo,A_courseinfo,A_studentcourse
whereA_studentcourse.sno=A_studentinfo.sno
andA_o=A_o
andA_studentinfo.sname='张三'
4、查询3班"张三"的"测试管理"成绩,要求显示姓名、成绩
selectsnameas姓名,scoreas成绩
fromA_studentcourse,A_courseinfo,A_studentinfo
whereA_o=A_o
andA_studentcourse.sno=A_studentinfo.sno
andsubject='测试管理'
andclass='3'
andsname='张三'
5、查询所有2000年以前入学的,各班男生的各科考试平均成绩
selectclassas班级,avg(score)as男生平均成绩
fromA_studentcourse,A_courseinfo,A_studentinfo
whereA_o=A_o
and
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语句 写法