最新sql语句练习题及答案.docx

上传人:b****5 文档编号:3122962 上传时间:2022-11-17 格式:DOCX 页数:8 大小:17.41KB
下载 相关 举报
最新sql语句练习题及答案.docx_第1页
第1页 / 共8页
最新sql语句练习题及答案.docx_第2页
第2页 / 共8页
最新sql语句练习题及答案.docx_第3页
第3页 / 共8页
最新sql语句练习题及答案.docx_第4页
第4页 / 共8页
最新sql语句练习题及答案.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

最新sql语句练习题及答案.docx

《最新sql语句练习题及答案.docx》由会员分享,可在线阅读,更多相关《最新sql语句练习题及答案.docx(8页珍藏版)》请在冰豆网上搜索。

最新sql语句练习题及答案.docx

最新sql语句练习题及答案

 

sql语句练习题及答案

一在数据库school中建立student,sc,course表。

学生表、课程表、选课表属于数据库School,其各自的数据结构如下:

学生Student(Sno,Sname,Ssex,Sage,Sdept)

序号

列名

含义

数据类型

长度

1

Sno

学号

字符型(char)

6

2

Sname

姓名

字符型(varchar)

8

3

Ssex

性别

字符型(char)

2

4

Sage

年龄

整数(smallint)

5

sdept

系科

字符型(varchar)

15

课程表course(Cno,Cname,Cpno,Ccredit)

序号

列名

含义

数据类型

长度

1

Cno

课程号

字符型(char)

4

2

cname

课程名

字符型(varchar)

20

3

Cpno

先修课

字符型(char)

4

4

Ccredit

学分

短整数(tinyint)

学生选课SC(Sno,Cno,Grade)

序号

列名

含义

数据类型

长度

1

Sno

学号

字符型(char)

6

2

Cno

课程号

字符型(char)

4

3

Grade

成绩

小数(decimal)

12,2

二设定主码

1Student表的主码:

sno

2Course表的主码:

cno

3Sc表的主码:

sno,cno

1写出使用CreateTable语句创建表student,sc,course的SQL语句

2在student表中插入信息

学号

姓名

性别

年龄

系科

4001

赵茵

20

SX

4002

杨华

21

JSJ

3删除student表中的元组

4在数据库school中删除关系student

5在student表添加属性sbirthdate类型datetime

练习

Delete

1删除所有JSJ系的男生deletefromStudentwhereSdept=’JSJ’andSsex=’男’;

2删除“数据库原理”的课的选课纪录

deletefromSCwhereCnoin(selectCnofromCoursewhereCname=’数据库原理’);

Update

1修改0001学生的系科为:

JSJ

2把陈小明的年龄加1岁,性别改为女。

2修改李文庆的1001课程的成绩为93分

3把“数据库原理”课的成绩减去1分

Select查询语句

一单表

1查询年龄在19至21岁之间的女生的学号,姓名,年龄,按年龄从大到小排列。

2查询姓名中第2个字为“明”字的学生学号、性别。

3查询1001课程没有成绩的学生学号、课程号

4查询JSJ、SX、WL系的年龄大于25岁的学生学号,姓名,结果按系排列

5按10分制查询学生的sno,cno,10分制成绩

(1-10分为1,11-20分为2,30-39分为3,。

90-100为10)

6查询student表中的学生共分布在那几个系中。

(distinct)

7查询0001号学生1001,1002课程的成绩。

二统计

1查询姓名中有“明”字的学生人数。

2计算‘JSJ’系的平均年龄及最大年龄。

3查询学生中姓名为张明、赵英的人数

4计算每一门课的总分、平均分,最高分、最低分,按平均分由高到低排列

5计算1001,1002课程的平均分。

6查询平均分大于80分的学生学号及平均分

7统计选修课程超过2门的学生学号

8统计有10位成绩大于85分以上的课程号。

9统计平均分不及格的学生学号

10统计有大于两门课不及格的学生学号

三连接

1查询JSJ系的学生选修的课程号

2查询选修1002课程的学生的学生姓名(不用嵌套及嵌套2种方法)

3查询数据库原理不及格的学生学号及成绩

4查询选修“数据库原理”课且成绩80以上的学生姓名(不用嵌套及嵌套2种方法)

5查询平均分不及格的学生的学号,姓名,平均分。

6查询女学生平均分高于75分的学生姓名。

7查询男学生学号、姓名、课程号、成绩。

(一门课程也没有选修的男学生也要列出,不能遗漏)

四嵌套、相关及其他

1查询平均分不及格的学生人数

2查询没有选修1002课程的学生的学生姓名

3查询平均分最高的学生学号及平均分(2种方法TOP,any,all)

*4查询没有选修1001,1002课程的学生姓名。

5查询1002课程第一名的学生学号(2种方法)

6查询平均分前三名的学生学号

7查询JSJ系的学生与年龄不大于19岁的学生的差集

8查询1001号课程大于90分的学生学号、姓名及平均分大于85分的学生学号、姓名

9查询每门课程成绩都高于该门课程平均分的学生学号

10查询大于本系科平均年龄的学生姓名

答案

参考答案

 1createtablestudent

 (snochar(6),

 snamevarchar(8),

 ssexchar

(2),

 sagesmallint,

 sdeptvarchar(15),

 primarykey(sno));

 createtablesc

 (snochar(6),

 cnochar(4),

 gradedecimal(12,2),

 primarykey(sno,cno));

insertintostudent

values(’4001’,’赵茵’,’男’,20,’SX’)

deletefromstudent

droptablestudent

altertablestudentaddsbirthdatedatetime

1selectsno,sname,sage

fromstudent

wheressex=’女’andsagebetween19and21

orderbysagedesc;

2selectsno,ssex

fromstudent

wheresnamelike’_明%’;

3selectsno,cno

fromsc

wheregradeisnullandcno=’1001’;

4selectsno,sname

fromstudent

wheresdeptin(’JSJ’,’SX’,’WL’)andsage>25

groupbysdept;

selectsno,cno,grade/10.0+1aslevel

 fromsc;

selectdistinctsdeptfromstudent;

selectgrade

   fromsc

   wheresno=’0001’and(cno=’1001’orcno=’1002’);

selectcount(*)fromstudentwheresnamelike’%明%’;

selectavg(sage),max(sage)fromstudentwheresdept=’JSJ’;

selectcno,sum(grade),avg(grade),max(grade),min(grade)fromsc

groupbycno

orderbyavg(grade)desc;

selectcno,avg(grade)fromscwherecnoin(‘1001’,’1002’)

groupbycno;

selectsc.sno,avg(grade)fromsc

groupbysc.sno

havingavg(grade)>80;

selectsnofromscgroupbysnohavingcount(*)>2;

selectcnofromscwheregrade>85groupbycnohavingcount(*)=10;

selectsnofromscgroupbysnohavingavg(grade)<60;

selectsnofromscwheregrade<60groupbysnohavingcount(*)>2;

selectcnofromstudent,scwherestudent.sno=sc.snoandsdept=’JSJ’;

a:

selectsnamefromstudent,scwherestudent.sno=sc.snoandcno=’1002’

b:

selectsnamefromstudentwheresnoin(selectsnofromscwherecno=’1002’)

selectsno,gradefromsc,course

 whereo=oandcname=’数据库原理’andgrade<60

a:

selectsnamefromstudent,sc,course

 wherestudent.sno=sc.snoando=oandgrade>80andcname=’数据库原理’

   b:

selectsnamefromstudentwheresnoin(selectsnofromscwheregrade>80andcnoin

   (selectcnofromcoursewherecname=’数据库原理’))

selectsno,sname,avg(grade)fromsc,student

  wherestudent.sno=sc.sno

  groupbystudent.sno

  havingavg(grade)<60

a:

selectsnamefromstudentwheressex=’女’andsnoin(selectsnofromscgroupbysno

havingavg(grade)>75)

b:

selectsnamefromsc,studentwherestudent.sno=sc.snoandssex=’女’

groupbystudent.snohavingavg(grade)>75

selectstudent.sno,sname,cno,gradefromstudentleftjoinsconstudent.sno=sc.sno

   andssex=’男’

selectcount(*)fromstudentwheresnoin(selectsnofromscgroupbysnohaving

avg(grade)<60)

selectsnamefromstudentwheresnonotin(selectsnofromscwherecno=’1002’

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 法律文书 > 判决书

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1