当前位置:文档之家› SQL语句练习及答案

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,ssexfrom studentwhere sname not like “刘%”;4.查询db_design课程的课程号和学分。

select cno,ccredit from coursewhere 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,coursewhere sno= ' 200215012 ' and o=o;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 o=o;17.查询每个学生及其选修课程的情况包括没有选修课程的学生18.查询选修2号课程且成绩在90分以上的所有学生的学号、姓名select student.sno, student.snamefrom student,scwhere student.sno=sc.sno and o=”2‟and sc.grade>90;19.查询每个学生的学号、姓名、选修的课程名及成绩。

select student.sno,sname,ssex,sage,sdept,cno,gradefrom student left outjoin sco on(student.sno=sc.sno);20.查询与“刘晨”在同一个系学习的学生。

selectsno,sname,sdeptfrom studentwhere 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 asselect 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,sagefrom swhere sage<20;(not age>=20);2:查考试成绩有不及格的学生的学号select distinct snofrom scwhere grade<60;3:查所年龄在20至23岁之间的学生姓名、系别及年龄。

select sname,sdept,sagefrom swhere 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,snamefrom swhere sdept=’cs’ and sage<20;13:查计算机系、数学系、信息系的学生姓名、性别。

select sname,ssexfrom swhere sdept=’cs’ or sdept=’is’ or sdept=’math’;14:查询选修了c3课程的学生的学号和成绩,其结果按分数的降序排列。

select sno,gradefrom scwhere cno=’c3’order by grade desc;15:查询全体学生的情况,查询结果按所在系升序排列,对同一系中的学生按年龄降序排列。

select *from sorder by sdep,sage desc;16:查询学生总人数。

select count(*) from s;17:查询选修了课程的学生人数。

select count(distinct sno) from sc18:计算选修了c1课程的学生平均成绩。

select avg(grade)from scwhere cno=’c1’;19:查询学习c3课程的学生最高分数。

select max(grade)from scwhere cno=’c3’;20:查询各个课程号与相应的选课人数。

select cno, count(sno)from scgroup by cno;21:查询计算机系选修了3门以上课程的学生的学号。

相关主题