文档库 最新最全的文档下载
当前位置:文档库 › SQL语句练习及答案

SQL语句练习及答案

SQL语句练习及答案
SQL语句练习及答案

sql语句练习题1

数据库有如下四个表格:

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

系表(dptno, dname)

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

sc (sno, eno, 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课程的课程号和学分。

selecto, ccredit from course

whereame like ' db_design,

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

select * from course whereame like ' db%i_ ;

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

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

select sno, eno from sc where grade is null;

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

select sno, eno 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 whereo= ' 3 ' order by grade desc;

10.查询学生总人数。

select count(*) from student;

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

select count(distinct sno) from sc;

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

select avg (grade) from sc whereo= ' 1

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

select max(grade) from sc whereo= ' 1 ';

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

select sum(grade) from sc, course

where sno= 200215012 and https://www.wendangku.net/doc/615165840.html,o=course? eno:

13?查询选修了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? eno:

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

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

select student? sno, student? sname

from student, sc

where student?sno二sc?sno and sc. eno二"2'and sc.grade>90;

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

select student?sno, sname, ssex, sage, sdept, eno, grade

from student left out join sco on (student?sno二sc?sno):

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

selectsno, sname, sdept

from student

where sdept in

(select sdept from student where sname=n晨J ;

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

select sno, sname from student where sno in (select sno from sc whereo in

(selecto from course whereame=n信息系统'));

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

select sno, eno from sc x where grade>=

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

23.将一个新学生记录(学号:20021512&:冬;性别:男;所在系:is;年

龄: 18岁)插入到student表中。

insert into student values ('200213128','冬','男','is', 18);

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

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

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

update student setsage=sage+l;

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

update sc set grade二0 where exits

(selete * from student where student?sno=sc? sno and sdept二" iI?算朋l 禾}学系”);

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

delete from student where sno二"2002⑸28’ :

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

delete from sc;

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

delete from sc whereo=,2’ ;

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

delete from sc where sno in

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

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

create view is_student as

select sno, sname, sage from student where sdept二'is :

sql语句练习题2

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

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

学习关系sc (sno? eno, grade);

课程关系c (eno, 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* ):

相关文档