SQL语句练习及答案

更新时间:2023-11-27 23:03:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

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,ccredit from course where cname like 'db_design'

5. 查询以\开头,且倒数第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

where sno= ' 200215012 ' and sc.cno=course.cno;

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

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

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

select student.*,sc.*, course.* from student,sc , course where student.sno=sc.sno and sc.cno=course.cno;

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

18. 查询选修2号课程且成绩在90分以上的所有学生的学号、姓名 select student.sno, student.sname from student,sc

where student.sno=sc.sno and sc.cno=”2?and sc.grade>90;

19. 查询每个学生的学号、姓名、选修的课程名及成绩。 select student.sno,sname,ssex,sage,sdept,cno,grade from student left outjoin sco on(student.sno=sc.sno);

20. 查询与“刘晨”在同一个系学习的学生。 selectsno,sname,sdept from student where sdept in

(select sdept from student where sname=”刘晨?);

21. 查询选修了课程名为“信息系统”的学生学号和姓名 select sno,sname from student where sno in (select sno from sc where cno in

(select cno from course where cname=”信息系统?));

22. 找出每个学生超过他选修课程平均成绩的课程号。 select sno,cno from sc x where grade>=

(select avg(grade) from sc y where y.sno=x.sno);

23. 将一个新学生记录(学号:200215128;姓名:陈冬;性别:男;所在系:

is;年龄:18岁)插入到student表中。 insert into student values ('200215128','陈冬','男','is',18);

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

update student setsage=22 where sno='200215121';

25. 将所有学生的年龄增加1岁。 update student setsage=sage+1;

26. 将计算机科学系全体学生的成绩置零。 update sc set grade=0 where exits

(selete * from student where student.sno=sc.sno and sdept=” 计算机科学系”);

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

delete from student where sno=”200215128';

28. 删除所有的学生选课记录。 delete from sc;

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

delete from sc where cno='2';

30. 删除计算机科学系所有学生的选课记录。 delete from sc where sno in

(selete sno from student where sdept=” 计算机科学系”);

31. 建立信息系学生的视图。 create view is_student as

select sno,sname,sage from student where sdept='is'

sql语句练习题2

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

学生关系s(sno,sname,age,sex,sdept);

学习关系sc(sno,cno,grade);

课程关系c(cno,cname,cdept,tname)

查询问题:

1:查所有年龄在20岁以下的学生姓名及年龄。 select sname,sage from s

where sage<20;(not age>=20);

2:查考试成绩有不及格的学生的学号 select distinct sno from sc

where grade<60;

3:查所年龄在20至23岁之间的学生姓名、系别及年龄。 select sname,sdept,sage from s

where sage between 20 and 23;

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

select sname,ssex from s where sdept in(‘cs’,’is’,’math’);

5:查既不是计算机系、数学系、又不是信息系的学生姓名、性别 select sname,ssex from s where sdept not in(‘cs’,’is’,’math’);

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

select sname,sno,ssex from s where sname like‘刘%’;

7:查姓“上官”且全名为3个汉字的学生姓名。 select sname from s where sname like ‘上官__’;

8:查所有不姓“张”的学生的姓名。

select sname,sno,ssex from s where sname not like

9:查db_design课程的课程号。

select cno from c where cname like ‘db_design’;

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

select sno,cno from sc where grade is null;

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

select sno,sname from s where sage is null;

12:查计算机系20岁以下的学生的学号和姓名。 select sno,sname from s

where sdept=’cs’ and sage<20;

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

%’;‘张 select sname,ssex from s

where sdept=’cs’ or sdept=’is’ or sdept=’math’;

14:查询选修了c3课程的学生的学号和成绩,其结果按分数的 降序排列。 select sno,grade from sc

where cno=’c3’ order by grade desc;

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

order by sdep,sage desc;

16:查询学生总人数。 select count(*) from s;

17:查询选修了课程的学生人数。 select count(distinct sno) from sc

18:计算选修了c1课程的学生平均成绩。 select avg(grade) from sc

where cno=’c1’;

19:查询学习c3课程的学生最高分数。 select max(grade) from sc

where cno=’c3’;

20:查询各个课程号与相应的选课人数。 select cno, count(sno) from sc group by cno;

21:查询计算机系选修了3门以上课程的学生的学号。 select sno from sc

where sdept=’cs’ group by sno having count(*)>3;

22:求基本表s中男同学的每一年龄组(超过50人)有多少人? 要求查询结果按人数升序排列,人数相同按年龄降序排列。 select sage,count(sno) from s

where ssex='m' group by sage having count(*)>50 order by 2,sage desc;

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

select s.sno, sname, sage, ssex, sdept, cno, grade from s, sc

where s.sno=sc.sno;

24:查询选修了c2课程且成绩在90分以上的所有学生。 select s.sno,sname from s,sc

where s.sno=sc.sno and sc.cno=’c2’ and sc.grade>90;

25:查询每个学生选修的课程名及其成绩。 select s.sno,sname,cname,sc.grade from s,sc,c

where s.sno=sc.sno and sc.cno=c.cno

26:统计每一年龄选修课程的学生人数。 select sage,count(distinct s.sno) from s,sc

where s.sno=sc.sno group by sage;

27:查询选修了c2课程的学生姓名。 select sname from s where sno in(

select sno from sc where cno=’c2’);

28:查询与“张三”在同一个系学习的学生学号、姓名和系别。 select sno,sname,sdept from where sdept=( select sdept from s where sname=’张三’);

29:查询选修课程名为“数据库”的学生学号和姓名。 select sno,sname from s where sno in ( select sno from sc where cno in ( select cno from c where cname=’db’));

30:查询与“张三”在同一个系学习的学生学号、姓名和系别。 select sno,sname,sdept from s where sdept=( select sdept from s where sname=‘张三’);

31:查询选修课程名为“数据库”的学生学号和姓名。 select sno,sname from s where sno in (select sno from sc where cno=

(select cno from c where cname=’db’));

32:查询选修了c2课程的学生姓名。 1. select sname from s where sno in (select sno from sc where cno=’c2’); 2. select sname from s where exists

(select * from sc where sc.sno=s.sno and cno=’c2’);

33:查询选修了全部课程的学生姓名。 select sname from s where not exists (select * from c where not exists

(select * from sc where sc.sno=s.sno and sc.cno=c.cno));

36:查询所学课程包含学生s3所学课程的学生学号 select distinct sno from sc as x where not exists

(select * from sc as y where y.sno=’s3’ and not exists (select * from sc as z where z.sno=x.sno and z.cno=y.cno));

sql语句练习题 3

一、简单查询

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

2、列出软件专业全部学生的学号及姓名。 select学号,姓名from学生where专业=\软件\ 3、列出所有必修课的课号。 select distinct课号from必修课

4、求1号课成绩大于80分的学生的学号及成绩,并按成绩由高到低列出。 select学号,成绩from选课where课号=\成绩>80 order by成绩desc 5、列出非软件专业学生的名单。

方法一:select姓名from学生where专业<>\软件\ 方法二:select姓名from学生where not专业=\软件\ 方法三:select姓名from学生where专业!=\软件\ 6、查询成绩在70~80分之间的学生选课得分情况 方法一:select*from选课where成绩>=70and成绩<=80 方法二:select*from选课where成绩between70and80 不在此范围内的查询:(注意写出和以下语句等价的语句) select * from 选课 where成绩not between70and80 7、列出选修1号课或3号课的全体学生的学号和成绩。 方法一:select学号,成绩from选课where课号=\课号=\ 方法二:select学号,成绩from选课where课号in(\

相反条件查询:select学号,成绩from选课where课号notin(\

8、列出所有98级学生的学生成绩情况。 select*from选课where学号like\ select*from选课where学号like\

相反条件查询:select*from选课where学号notlike\ 9、列出成绩为空值(或不为空值)的学生的学号和课号。 答案一:select学号,课号from选课where成绩isnull 答案二:select学号,课号from选课where成绩isnotnull 10、求出所有学生的总成绩。

select sum(成绩) as总成绩 from 选课 11、列出每个学生的平均成绩。

select学号,avg(成绩) as 平均成绩from选课group by学号 12、列出各科的平均成绩、最高成绩、最低成绩和选课人数。 select课号,avg(成绩)as平均成绩,max(成绩)as最高分,;

min(成绩)as最低分,count(学号) as 选课人数 from 选课 group by 课号 二、连接查询 (一)简单连接

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

select 姓名,成绩 from学生,选课 where学生.学号=选课.学号 and 课号=\ 2、列出选修1号课的学生的学号、姓名及成绩。

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

3、求出总分大于150的学生的学号、姓名及总成绩。 select学生.学号,姓名,sum(成绩)as总成绩from学生,选课; where学生.学号=选课.学号groupby选课.学号havingsum(成绩)>150

(二)自连接查询

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

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

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

selectx.学号from选课x,选课ywherex.学号=y.学号andx.课号=\课号=\

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\领导\雇员姓名,\雇员\雇员姓名from雇员s,雇员ewheres.雇员号=e.经理 (三)超连接

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

本文来源:https://www.bwwdw.com/article/ns4t.html

Top