SQL语句练习及答案.docx

上传人:b****1 文档编号:11163917 上传时间:2023-05-29 格式:DOCX 页数:19 大小:21.18KB
下载 相关 举报
SQL语句练习及答案.docx_第1页
第1页 / 共19页
SQL语句练习及答案.docx_第2页
第2页 / 共19页
SQL语句练习及答案.docx_第3页
第3页 / 共19页
SQL语句练习及答案.docx_第4页
第4页 / 共19页
SQL语句练习及答案.docx_第5页
第5页 / 共19页
SQL语句练习及答案.docx_第6页
第6页 / 共19页
SQL语句练习及答案.docx_第7页
第7页 / 共19页
SQL语句练习及答案.docx_第8页
第8页 / 共19页
SQL语句练习及答案.docx_第9页
第9页 / 共19页
SQL语句练习及答案.docx_第10页
第10页 / 共19页
SQL语句练习及答案.docx_第11页
第11页 / 共19页
SQL语句练习及答案.docx_第12页
第12页 / 共19页
SQL语句练习及答案.docx_第13页
第13页 / 共19页
SQL语句练习及答案.docx_第14页
第14页 / 共19页
SQL语句练习及答案.docx_第15页
第15页 / 共19页
SQL语句练习及答案.docx_第16页
第16页 / 共19页
SQL语句练习及答案.docx_第17页
第17页 / 共19页
SQL语句练习及答案.docx_第18页
第18页 / 共19页
SQL语句练习及答案.docx_第19页
第19页 / 共19页
亲,该文档总共19页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

SQL语句练习及答案.docx

《SQL语句练习及答案.docx》由会员分享,可在线阅读,更多相关《SQL语句练习及答案.docx(19页珍藏版)》请在冰点文库上搜索。

SQL语句练习及答案.docx

SQL语句练习及答案

sql语句练习题1

数据库有如下四个表格:

student(sno,sname,sage,ssex,sdpt)学生表

系表〔dptno,dname〕

course(cno,cname,gradet,tno)课程表

sc(sno,cno,score)成绩表

teacher(tno,tname)教师表

要求:

完成以下操作

1.查询姓"欧阳"且全名为三个汉字的学生的姓名。

select sname from student where sname like “欧阳__‟;

2.查询名字中第2个字为"阳"字的学生的姓名和学号。

 select sname,sno from student where sname like '_阳%';

3.查询所有不姓刘的学生姓名。

select sname,sno,ssex 

from student 

where sname not like “刘%〞;

4.查询db_design课程的课程号和学分。

 select cno,ccreditfrom course 

where cname like 'db_design' 

5.查询以"db_"开头,且倒数第3个字符为i的课程的详细情况。

 select * from course where cname like 'db%i_ _';

6.某些学生选修课程后没有参加考试,所以有选课记录,但没有考试成绩。

查询缺少成绩的学生的学号和相应的课程号。

select sno,cno from sc where grade is null;

7.查所有有成绩的学生学号和课程号。

select sno,cno from sc where grade is not null;

8.查询计算机系年龄在20岁以下的学生姓名。

 select sname from student where sdept= 'cs' and sage<20;

9.查询选修了3号课程的学生的学号及其成绩,查询结果按分数降序排列。

 select sno,grade from sc where cno= ' 3 ' order by grade desc;

10.查询学生总人数。

select count(*) from student;

11.查询选修了课程的学生人数。

select count(distinct sno) from sc;

12.计算1号课程的学生平均成绩。

select avg(grade) from sc where cno= ' 1 ';

13.查询选修1号课程的学生最高分数。

select max(grade) from sc where cno= ' 1 ';

14.查询学生200215012选修课程的总学分数。

select sum(grade) from sc,course 

wheresno= ' 200215012 ' and sco=courseo;

15.查询选修了3门以上课程的学生学号。

select sno from sc group by sno having count(*) >3; 

16.查询每个学生及其选修课程的情况。

selectstudent.*,sc.*,course.*fromstudent,sc,course

wherestudent.sno=sc.snoandsco=courseo;

17.查询每个学生及其选修课程的情况包括没有选修课程的学生

18.查询选修2号课程且成绩在90分以上的所有学生的学号、姓名

selectstudent.sno,student.sname

fromstudent,sc

wherestudent.sno=sc.snoandsco=〞2‟andsc.grade>90;

19.查询每个学生的学号、姓名、选修的课程名及成绩。

selectstudent.sno,sname,ssex,sage,sdept,cno,grade

fromstudentleftoutjoinscoon(student.sno=sc.sno);

20.查询与“刘晨〞在同一个系学习的学生。

selectsno,sname,sdept

fromstudent

wheresdeptin

(selectsdeptfromstudentwheresname=〞刘晨‟);

21.查询选修了课程名为“信息系统〞的学生学号和姓名

selectsno,snamefromstudentwheresnoin

(selectsnofromscwherecnoin

(selectcnofromcoursewherecname=〞信息系统‟));

22.找出每个学生超过他选修课程平均成绩的课程号。

selectsno,cnofromscxwheregrade>=

(selectavg(grade)fromscywherey.sno=x.sno);

23.将一个新学生记录〔学号:

200215128;姓名:

陈冬;性别:

男;所在系:

is;年龄:

18岁〕插入到student表中。

insertintostudentvalues('200215128','陈冬','男','is',18);

24.将学生200215121的年龄改为22岁。

updatestudentsetsage=22wheresno='200215121';

25.将所有学生的年龄增加1岁。

updatestudentsetsage=sage+1;

26.将计算机科学系全体学生的成绩置零。

updatescsetgrade=0whereexits

(selete*fromstudentwherestudent.sno=sc.snoandsdept=〞计算机科学系〞);

27.删除学号为20021528的学生记录

deletefromstudentwheresno=〞200215128';

28.删除所有的学生选课记录。

deletefromsc;

29.删除2号课程的所有选课记录。

deletefromscwherecno='2';

30.删除计算机科学系所有学生的选课记录。

deletefromscwheresnoin

(seletesnofromstudentwheresdept=〞计算机科学系〞);

31.建立信息系学生的视图。

 createviewis_studentas

selectsno,sname,sagefromstudentwheresdept='is';

sql语句练习题2

设教学数据库education,有三个关系:

 

学生关系s〔sno,sname,age,sex,sdept〕;

 

学习关系sc〔sno,cno,grade〕;

 

课程关系c〔cno,cname,cdept,tname〕

 

查询问题:

 

1:

查所有年龄在20岁以下的学生姓名及年龄。

selectsname,sage

froms

wheresage<20;(notage>=20);

2:

查考试成绩有不及格的学生的学号 

selectdistinctsno

fromsc

wheregrade<60;

3:

查所年龄在20至23岁之间的学生姓名、系别及年龄。

selectsname,sdept,sage

froms

wheresagebetween20and23;

4:

查计算机系、数学系、信息系的学生姓名、性别。

 selectsname,ssexfromswheresdeptin〔‘cs’,’is’,’math’〕;

5:

查既不是计算机系、数学系、又不是信息系的学生姓名、性别

 selectsname,ssexfromswheresdeptnotin〔‘cs’,’is’,’math’〕;

 

6:

查所有姓“刘〞的学生的姓名、学号和性别。

 selectsname,sno,ssexfromswheresnamelike‘刘%’;

7:

查姓“上官〞且全名为3个汉字的学生姓名。

 selectsnamefromswheresnamelike‘上官__’;

8:

查所有不姓“张〞的学生的姓名。

 selectsname,sno,ssexfromswheresnamenotlike‘张%’;

9:

查db_design课程的课程号。

selectcnofromcwherecnamelike‘db_design’;

10:

查缺考的学生的学号和课程号。

 selectsno,cnofromscwheregradeisnull;

11:

查年龄为空值的学生的学号和姓名。

 selectsno,snamefromswheresageisnull;

12:

查计算机系20岁以下的学生的学号和姓名。

selectsno,sname

froms

wheresdept=’cs’andsage<20;

13:

查计算机系、数学系、信息系的学生姓名、性别。

selectsname,ssex

froms

wheresdept=’cs’orsdept=’is’orsdept=’math’;

14:

查询选修了c3课程的学生的学号和成绩,其结果按分数的

降序排列。

selectsno,grade

fromsc

wherecno=’c3’

orderbygradedesc;

15:

查询全体学生的情况,查询结果按所在系升序排列,对同一系中的学生按年龄降序排列。

select*

froms

orderbysdep,sagedesc;

16:

查询学生总人数。

selectcount(*)froms;

17:

查询选修了课程的学生人数。

 selectcount(distinctsno)fromsc

18:

计算选修了c1课程的学生平均成绩。

selectavg(grade)

fromsc

wherecno=’c1’;

19:

查询学习c3课程的学生最高分数。

selectmax(grade)

fromsc

wherecno=’c3’;

20:

查询各个课程号与相应的选课人数。

selectcno,count(sno)

fromsc

groupbycno;

 

21:

查询计算机系选修了3门以上课程的学生的学号。

selectsno

fromsc

wheresdept=’cs’

groupbysno

havingcount(*)>3;

 

22:

求根本表s中男同学的每一年龄组〔超过50人〕有多少人?

要求查询结果按人数升序排列,人数相同按年龄降序排列。

selectsage,count〔sno〕

froms

wheressex='m'

groupbysage

havingcount〔*〕>50

orderby2,sagedesc;

23:

查询每个学生及其选修课程的情况。

selects.sno,sname,sage,ssex,sdept,cno,grade

froms,sc

wheres.sno=sc.sno;

24:

查询选修了c2课程且成绩在90分以上的所有学生。

selects.sno,sname

froms,sc

wheres.sno=sc.sno

andsco=’c2’

andsc.grade>90;

 

25:

查询每个学生选修的课程名及其成绩。

selects.sno,sname,cname,sc.grade

froms,sc,c

wheres.sno=sc.snoandsco=co

 

26:

统计每一年龄选修课程的学生人数。

 selectsage,count〔distincts.sno〕

froms,sc

wheres.sno=sc.sno

groupbysage;

27:

查询选修了c2课程的学生姓名。

selectsnamefromswheresnoin(

selectsnofromscwherecno=’c2’);

28:

查询与“张三〞在同一个系学习的学生学号、姓名和系别。

selectsno,sname,sdeptfromwheresdept=(

selectsdeptfromswheresname=’张三’);

 

29:

查询选修课程名为“数据库〞的学生学号和姓名。

selectsno,snamefromswheresnoin〔

selectsnofromscwherecnoin(

selectcnofromcwherecname=’db’));

 

30:

查询与“张三〞在同一个系学习的学生学号、姓名和系别。

selectsno,sname,sdeptfromswheresdept=〔

selectsdeptfromswheresname=‘张三’〕;

 

31:

查询选修课程名为“数据库〞的学生学号和姓名。

selectsno,snamefromswheresnoin

〔selectsnofromscwherecno=

〔selectcnofromcwherecname=’db’〕〕;

32:

查询选修了c2课程的学生姓名。

1.selectsnamefromswheresnoin

〔selectsnofromscwherecno=’c2’);

2.selectsnamefromswhereexists

〔select*fromscwheresc.sno=s.snoandcno=’c2’);

 

33:

查询选修了全部课程的学生姓名。

selectsnamefromswherenotexists

〔select*fromcwherenotexists

〔select*fromscwheresc.sno=s.snoandsco=co));

 

36:

查询所学课程包含学生s3所学课程的学生学号 

selectdistinctsnofromscasxwherenotexists

〔select*fromscasywherey.sno=’s3’andnotexists

〔select*fromscaszwherez.sno=x.snoandzo=yo));

sql语句练习题3

一、简单查询

1、列出全部学生的信息。

select*from学生

2、列出软件专业全部学生的学号及姓名。

select学号,姓名from学生where专业="软件"

3、列出所有必修课的课号。

selectdistinct课号from必修课

4、求1号课成绩大于80分的学生的学号及成绩,并按成绩由高到低列出。

select学号,成绩from选课where课号="1"and成绩>80orderby成绩desc

5、列出非软件专业学生的名单。

方法一:

select姓名from学生where专业<>"软件"

方法二:

select姓名from学生wherenot专业="软件"

方法三:

select姓名from学生where专业!

="软件"

6、查询成绩在70~80分之间的学生选课得分情况

方法一:

select*from选课where成绩>=70and成绩<=80

方法二:

select*from选课where成绩between70and80

不在此范围内的查询:

〔注意写出和以下语句等价的语句〕

select*from选课where成绩notbetween70and80

7、列出选修1号课或3号课的全体学生的学号和成绩。

方法一:

select学号,成绩from选课where课号="1"or课号="3"

方法二:

select学号,成绩from选课where课号in("1","3")

相反条件查询:

select学号,成绩from选课where课号notin("1","3")

8、列出所有98级学生的学生成绩情况。

select*from选课where学号like"98%"

select*from选课where学号like"98____"

相反条件查询:

select*from选课where学号notlike"98%"

9、列出成绩为空值(或不为空值)的学生的学号和课号。

答案一:

select学号,课号from选课where成绩isnull

答案二:

select学号,课号from选课where成绩isnotnull

10、求出所有学生的总成绩。

selectsum(成绩)as总成绩from选课

11、列出每个学生的平均成绩。

select学号,avg(成绩)as平均成绩from选课groupby学号

12、列出各科的平均成绩、最高成绩、最低成绩和选课人数。

select课号,avg(成绩)as平均成绩,max(成绩)as最高分,;

min(成绩)as最低分,count(学号)as选课人数from选课groupby课号

二、连接查询

〔一〕简单连接

1、列出选修1号课的学生姓名及成绩。

select姓名,成绩from学生,选课where学生.学号=选课.学号and课号="1"

2、列出选修1号课的学生的学号、姓名及成绩。

select学生.学号,姓名,成绩from学生s,选课xwheres.学号=x.学号and课号="1"

3、求出总分大于150的学生的学号、姓名及总成绩。

select学生.学号,姓名,sum(成绩)as总成绩from学生,选课;

where学生.学号=选课.学号groupby选课.学号havingsum(成绩)>150

〔二〕自连接查询

1、列出那些专业相同的学生相应的姓名及专业信息。

selecta.姓名,b.姓名,专业from学生a,学生bwherea.学号<>b.学号anda.专业=b.专业

2、求至少选修1号课和2号课的学生的学号。

selectx.学号from选课x,选课ywherex.学号=y.学号andx.课号="1"andy.课号="2"

3、有以下表rate.dbf

币种1代码c

(2)、币种2代码c

(2)、买入价n(8,4)、卖出价n(8,4)

外汇汇率.dbf

币种1c(4)、币种2c(4)、买入价n(8,4)、卖出价n(8,4)

外汇代码.dbf

外汇名称c(10)、外汇代码c(10)

要求:

将所有“外汇汇率〞表中的数据插入rate表中并且顺序不变,由于“外汇汇率〞中的币种1和币种2存放的是外币名称,而rate表中的币种1代码和币种2代码应该存放外币代码,所以插入时要做相应的改动,外币名称与外向代码的对应关系存储在“外汇代码〞表中。

selecta.外币代码as币种1代码,b.外币代码as币种2代码,;

买入价,卖出价from外汇代码a,外汇汇率,外汇代码b;

wherea.外币名称=外汇汇率.币种1andb.外币名称=外汇汇率.币种2intotablerate

4、假定有“雇员〞表〔雇员号c〔2〕,雇员姓名c〔6〕,经理号c〔2〕〕,根据雇员关系列出上一级经理及其所领导的职员清单。

〔教案中的例题〕

select"领导",s.雇员姓名,"雇员",e.雇员姓名from雇员s,雇员ewheres.雇员号=e.经理

〔三〕超连接

1、列出选修1号课的学生姓名及成绩。

方法一:

〔使用简单连接查询格式〕

select姓名,成绩from学生,选课where学生.学号=选课.学号and课号="1"

方法二:

〔使用内部连接格式〕

select姓名,成绩from学生innerjoin选课on学生.学号=选课.学号where课号="1"

方法三:

内部连接的inner短语可以省略。

〔与方法二等价〕

select姓名,成绩from学生join选课on学生.学号=选课.学号where课号="1"

2、查询订货管理数据库中数据的仓库号、城市、供给商名和地址信息。

方法一:

使用简单连接格式。

select仓库.仓库号,城市,供给商名,地址from供给商,订购单,职工,仓库;

where供给商.供给商号=订购单.供给商号and订购单.职工号=职工.职工号;

and职工.仓库号=仓库.仓库号

方法二:

使用超连接的内部连接格式。

〔注意连接条件的顺序〕

select仓库.仓库号,城市,供给商名,地址from供给商join订购单join职工join仓库;

on职工.仓库号=仓库.仓库号on订购单.职工号=职工.职工号on供给商.供给商号=订购单.供给商号

3、查询没有选修任何课程的学生姓名。

方法一:

使用嵌套查询

select姓名from学生where学号notin(select学号from选课)

方法二:

使用超连接的右连接。

select姓名from选课rightjoin学生on选课.学号=学生.学号where选课.学号<>学生.学号

方法三:

使用超连接的左连接。

〔注意表名顺序和方法二的不同〕

select姓名from学生leftjoin选课on选课.学号=学生.学号where选课.学号<>学生.学号

三、嵌套查询

〔一〕普通嵌套与谓词exists

1、列出选修汇编语言课的学生的学号。

方法一:

select学号from选课where课号=(select课号from课程where课名="汇编语言")

方法二:

使用谓词exists。

注意和方法一格式上的不同。

select学号from选课whereexist(select*from课程;

where课名="汇编语言"and选课.课号=课程.课号)

2、求软件专业所有必修课的课程信息。

方法一:

select*from课程where课号in;(select课号from必修课where必修专业="软件")

方法二:

select*from课程whereexist(select*from必修课where必修专业="软件";

and课程.课号=必修课.课号)

〔二〕量词any、some、all

1、求选修2号课的学生中,成绩比选修1号课的最低成绩要高的学生的学号和成绩。

方法一:

select学号,成绩from选课where课号="2"and成绩>;

(selectmin(成绩)from选课where课号="1")

方法二:

any等价于some,所以可将any换成some。

select学号,成绩from选课where课号="2"and成绩>any;

(select成绩from选课where课号="1")

2、求选修2号课的学生中,成绩比选修1号课的任何学生的成绩都要高的那些学生的学号和成绩。

方法一:

sele

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

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2