当前位置:文档之家› sql练习题+答案

sql练习题+答案

(一) 新建以下几个表student(学生表):其中约束如下:(1)学号不能存在相同的(2)名字为非空(3)性别的值只能是’男’或’女’(4)系包括这几个:信息系,计算机科学系,数学系,管理系,中文系,外语系,法学系(5)出生日期为日期格式(6)年龄为数值型,且在0~100之间create table student (age smallint constraint d check (age between 0cs(成绩表):其中约束如下:(1)sno和cno分别参照student和course表中的sno,cno的字段(2)cj(成绩)只能在0〜100之间,可以不输入值create table cs (sno smallint not null referencesstudent ( sno ), ----定义成外键cno smallint not null referencescourse ( cno ), ----定义成外键cj smallint constraint e check (cj between0 and 100 ),---- 检查约束一—j(成绩)只能在~100之间,可以不输入值constraint f primary key ( sno , cno )---- 定义学生学号和课程号为sc表的主键)course(课程表)其约束如下:(1)课程号(cno)不能有重复的(2)课程名(cname非空(三)针对学生课程数据库查询(1)查询全体学生的学号与姓名。

Select sno , sname from student(2)查询全体学生的姓名、学号、所在系,并用别名显示出结果。

(3)查询全体学生的详细记录。

select * from student(4)查全体学生的姓名及其出生年份。

select sname , birth from student(5)查询学校中有哪些系。

select distinct dept from student(6)查询选修了课程的学生学号。

select sno from cs where eno is not null(7)查询所有年龄在20岁以下的学生姓名及其年龄。

select sname , age from student where age < 20(8)查询年龄在20~23岁(包括20岁和23岁)之间的学生的姓名、系别和年龄。

(9)查询年龄不在20~23岁之间的学生姓名、系别和年龄。

(10)查询信息系、数学系和计算机科学系生的姓名和性别。

(11)查询既不是信息系、数学系,也不是计算机科学系的学生的姓名和性别。

(18)查询以"DB_"开头,且倒数第3个字符为i 的课程的详细情况。

(12) 查询所有姓刘学生的姓名、学号和性别。

(13) 查询学号为2009011的学生的详细情况。

(具体的学号值根据表中数据 确定) select * from student where sno =5(14) 查询姓“欧阳”且全名为三个汉字的学生姓名(15)查询名字中第2个字为“晨”字的学生的姓名和学号(16)查询所有不姓刘的学生姓名。

(17)查询sql 课程的课程号和学分(19)查询缺少成绩的学生的学号和相应的课程号。

(20)查所有有成绩的学生学号和课程号。

(21)查询计算机系年龄在20岁以下的学生姓名。

(22)查询信息系、数学系和计算机科学系学生的姓名和性别。

(使用多个条件表达式)select sname , sex from student where dept =' 信息系’or dept ='数学系’or dept ='计算机科学系(23)查询年龄在20~23岁(包括20岁和23岁)之间的学生的姓名、系别和年龄。

(使用多个条件表达式)(24)查询选修了3号课程的学生的学号及其成绩,查询结果按分数降序排列between 20 and 23(24)查询选修了3号课程的学生的学号及其成绩,查询结果按分数降序排列(25)查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列。

(26)查询学生总人数。

select count (*) from student(27)查询选修了课程的学生人数。

select count (sno ) from cs where cno is not null(28)计算1号课程的学生平均成绩。

select avg ( cj ) from cs where cno = 1(29)查询选修1号课程的学生最高分数。

select max( cj ) from cs where cno =1(30)求各个课程号及相应的选课人数。

(31)查询选修了3门以上课程的学生学号。

select sno , count ( eno ) from es group by snohaving count ( eno )> 3(32)查询有3门以上课程是90分以上的学生的学号及(90分以上的)课程数。

select sno , count (eno ) as '课程数'from cswhere cj >90group by sno having count ( eno )>= 3(33)查询学生2006011选修课程的总学分(34)查询每个学生选修课程的总学分。

select sno , sum( cj ) from cs , coursewhere cs . eno =course . enogroup by sno(35)查询每个学生及其选修课程的情况。

select cs . sno , course .* from cs , course where cs . cno=course . cno(36)查询选修2号课程且成绩在90分以上的所有学生的学号、姓名select sno , sname from student wheresno =( select sno from cs where cno =2 and cj >90)(37)查询每个学生的学号、姓名、选修的课程名及成绩selectstudent .sno , sname , course . course , cs . cjfrom student ,course ,cs wherestudent .sno =cs . sno and cs . cno =course . cno (38)查询与“刘晨”在同一个系学习的学生(分别用嵌套查询和连接查询)')连接查询(39)查询选修了课程名为“信息系统”的学生学号和姓名(40)查询其他系中比信息系任意一个(其中某一个)学生年龄小的学生姓名和年龄(41)查询其他系中比信息系所有学生年龄都小的学生姓名及年龄。

分别用ALL胃词和集函数----用ALLselect sname , age from student where age vail (select agefrom student where dept ='信息系')---- 聚合函数select sname ,age from student where age < (select min ( age ) from student where dept =' 信息系')(42)查询所有选修了1号课程的学生姓名。

(分别用嵌套查询和连查询)---- 嵌套查询select sname from student where sno in (select sno from cs where cno =1)---- 连接查询select sname from student ,cswhere student .sno =cs . sno and cs . cno : =1(43)查询没有选修1号课程的学生姓名。

select sname from student where sno in(select sno from cs where eno != 1)(44)查询选修了全部课程的学生姓名。

(45)查询至少选修了学生95002选修的全部课程的学生号码select distinct sno from sc scx where not exists(select * from cs scy where scy .sno ='95002' and not exists(select * from sc scz where scz .sno =scx . sno and scz .cno =scy .cno ))(46)查询计算机科学系的学生及年龄不大于19岁的学生的信息。

(47)查询选修了课程1或者选修了课程2的学生的信息。

(48)查询计算机科学系中年龄不大于19岁的学生的信息。

select * from student where age <= 19 and dept ='计算机科学系'(49)查询既选修了课程1又选修了课程2的学生的信息select * from student where sno in(select sno from cs where cno ='003' and sno in( select sno from cs where cno = '004'))---- 用exists 查询select * from student where exists (select * from cs where student .sno =cs . sno and cno ='003' and sno in(select sno from cs where cno = '004'))(50)查询计算机科学系的学生与年龄不大于19岁的学生的差集。

select * from student where dept ='计算机科学系'and age >19(51)通过查询求学号为1学生的总分和平均分。

select sum( cj ) as '总分',avg ( cj )'平均分'from cs where sno =1 (52)求出每个系的学生数量select dept , count (sno ) as '学生个数'from student group by dept(53)查询平均成绩大于85的学生学号及平均成绩。

select sno , avg ( cj ) from cs group by sno having avg ( cj )> 85(54)要求查寻学生的所有信息,并且查询的信息按照年龄由高到低排序,如果年龄相等,则按照学号从低到高排序1.在SELECT 语句中DISTINCT、ORDER BY、GROUP BY 和HAVING 子句的功能各是什么?答各子句的功能如下。

相关主题