SQL精选习题<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
1、 创建数据库 MYDB
解答:create database MYDB
2、 创建学生表student (sno,sname,ssex,sage,sclass)
解答:create table student
(sno int primary key ,
sname varchar(8),
ssex varchar(3),
sage int ,
sclass varchar (6))
3、 创建课程表 course(cno,cname,ccredit)
解答:create table course
(cno int primary key,
cname varchar(20),
ccredit int )
4、 创建选课表SC(sno,cno,grade)
解答:create table sc
(sno int foreign key references student(sno),
cno int foreign key references course(cno),
grade int)
5、 添加数据(student)
1
李勇
男
20
Y01
2
刘晨
男
21
Y02
3
王敏
女
19
Y02
4
张力
男
25
Y05
解答:insert into student alues (1,'李勇','男',20,'y01')
insert into student values (2,'刘晨','男',21,'y02')
insert into student values (3,'王敏','女',19,'y02')
insert into student values (4,'张力','男',20,'y05')
6、 添加数据(course
1
C语言
5
2
数据库
5
3
开发模式_VB
5
答:insert into course values (1,'数据库',5)
insert into student values (2,'C语言',5')
insert into course values (3,'开发模式-VB',5)
7、 添加数据(SC)
1
1
90
1
2
95
2
1
55
4
3
null
解答:insert into SC values (1,1,5)
insert into SC values (1,2,5)
insert into SC values (1,1,5)
insert into SC values (1,3,5)
8、 查询全体同学的学号,姓名
解答:select sno,sname from student
9、 查询全体同学的姓名学号班级(按顺序输出)
解答:select sname,sno,sclass from student
10、 查询全体同学的记录(显示所有行)
解答:select *from student
11、 查询全体同学的姓名及出生年份
解答:select sname,2006-sage 出生年份 from student
12、 查询全体同学姓名出生年份班级(班级要用小写字母LOWER函数)
解答:select sname,2006-sage 出生年份 ,lower(sclass) from student
13、 查询全体同学的姓名/出生年份/所在班级列为YearOfBirth
解答:select sname,2006-sage YearOfBirth ,sclass from student
14、 查询选课中学员的学号并且去掉重复行用distinct
解答:select distinct sno from sc
15、 查Y02班全体同学名单
解答:select sno from student where sclass='y02'
16、 查所有年龄在20岁以下的同学姓名及年龄
解答:select sname,sage from student where sage<20
17、 查考试不合格的同学学号
解答:select sno from sc where grade<60
18、 查年龄在19-20岁(包括19-20)之间的同学姓名班级年龄
解答:select sname,sclass,sage from student where sage>=19 and sage<=20
19、 查年龄不在19-20岁之间的同学的姓名,班级,年龄
解答:select sname,sclass,sage from student
where sage not between 19 and 20
20、 查Y02班和Y05班的同学姓名,性别
解答:select sname ,ssex from student
where sclass='y02' or sclass='y05'
21、 查不是Y02班和Y05班的同学的姓名,性别
解答:select sname,ssex from student where
not sclass='y02' and not sclass='y05'
22、 查所有姓刘的同学的姓名,学号,性别
解答:select sname,sno,ssex from student where sname like '刘%'
23、 查所有姓张且全名为2个汉字的学生姓名
解答:select sname,sno,ssex from student where sname like '张_'
24、 某些学生未考试查缺少成绩的同学的学号和课程号
解答:select sno,cno from sc where grade is null
25、 查所有成绩的同学的学号,课程号,和成绩
解答:select sno,cno from sc where grade is not null
26、 查Y02班年龄在20岁以下的姓名和年龄
解答:select sname,sage from student where sclass='y02' and sage<20
27、 查选修1号课程的同学的学号和成绩,按降序排列
解答:select sno,grade from sc where cno=1 order by grade desc
28、 查全体同学信息查询结果按所在班级的班级名称按降序排列,同班同学按年龄升序排列
解答:select *from student order by sclass desc , sage asc
29、 查学员总人数
解答:select count(*) from student
30、 查选修课程学员人数
解答:select count(*) from sc
31、 统计1号课的学员平均成绩
解答:select avg(grade) from SC where cno=1
32、 查选修1号课和同学最高成绩
解答:select max(grade) from SC where cno=1
33、 求各个课程号及相应选课人数
解答:select count(*) from SC group by cno
34、 查选取1门以上课程的同学学号
解答:select sno from SC group by sno having count(cno)>1
35、 查每个学员及其选修,课程情况
解答:select sno,cno from SC
36、 查每个学员及其选修课程情况对没有选课的也要输出
其姓名,学号,性别,班级
解答:select a.sno,a.sname,a.sage,a.ssex,a.sclass,sc.cno,sc.grade
from student a left outer join sc on a.sno=sc.sno order by a.sname
37、 查选取2号课程且成绩在90分以上同学
解答:select *from SC where cno=2 and grade>9
38、 查询每个同学学号姓名,选课程名称及成绩
解答:select a.sno,a.sname,sc.cno,sc.grade from
student a left outer join SC on a.sno=SC.sno order by SC.sno
39、 查与刘晨在一个班的同学
解答:select sname from student where sclass in
(select sclass from student where sname='刘晨')
40、 选取C语言的同学学号和姓名
解答:select sno,sname from student where sno in (select sno from SC
where cno in (select cno from course where cname='C语言'))
41、 查其他班中比Y02班某一同学大的同学姓名和年龄
解答:select sname,sage from student where sage>any
(select sage from student where sclass='Y02') and sclass<>'Y02'
42、 查其他班中比Y02班同学全部都大的同学姓名和年龄
解答:select sname,sage from student where sage>all
(select sage from student where sclass='Y02') and sclass<>'Y02'
43、 查选取1号课程的学员的姓名
解答:select sname from student where sno in
(select sno from SC where cno=1)
44、 查没有选取1号课程的学员的姓名
解答:select sname from student where sno not in
(select sno from SC where cno =1)
45、 查Y02班同学及年龄不大于19岁的学员(union)
解答:select *from student where sclass ='y02' union
select *from student where sage<=19
46、 查询选取1号课程或者2号课程的同学学号
解答:select distinct sno from SC where cno=1 or cno=2
47、 将4号学员的年龄改为23岁
解答:update student set sage=23 where sno=4
48、 将所有同学的年龄增加1岁
解答:update student set sage=sage+1
49、 Y02班的同学的成绩改为100分
解答:update sc set grade=100 where sno in
(select sno from student where sclass='y02')
50、 删除学号为1的同学记录
解答:delete from student where sno=1
1、 创建数据库 MYDB
解答:create database MYDB
2、 创建学生表student (sno,sname,ssex,sage,sclass)
解答:create table student
(sno int primary key ,
sname varchar(8),
ssex varchar(3),
sage int ,
sclass varchar (6))
3、 创建课程表 course(cno,cname,ccredit)
解答:create table course
(cno int primary key,
cname varchar(20),
ccredit int )
4、 创建选课表SC(sno,cno,grade)
解答:create table sc
(sno int foreign key references student(sno),
cno int foreign key references course(cno),
grade int)
5、 添加数据(student)
1
李勇
男
20
Y01
2
刘晨
男
21
Y02
3
王敏
女
19
Y02
4
张力
男
25
Y05
解答:insert into student alues (1,'李勇','男',20,'y01')
insert into student values (2,'刘晨','男',21,'y02')
insert into student values (3,'王敏','女',19,'y02')
insert into student values (4,'张力','男',20,'y05')
6、 添加数据(course
1
C语言
5
2
数据库
5
3
开发模式_VB
5
答:insert into course values (1,'数据库',5)
insert into student values (2,'C语言',5')
insert into course values (3,'开发模式-VB',5)
7、 添加数据(SC)
1
1
90
1
2
95
2
1
55
4
3
null
解答:insert into SC values (1,1,5)
insert into SC values (1,2,5)
insert into SC values (1,1,5)
insert into SC values (1,3,5)
8、 查询全体同学的学号,姓名
解答:select sno,sname from student
9、 查询全体同学的姓名学号班级(按顺序输出)
解答:select sname,sno,sclass from student
10、 查询全体同学的记录(显示所有行)
解答:select *from student
11、 查询全体同学的姓名及出生年份
解答:select sname,2006-sage 出生年份 from student
12、 查询全体同学姓名出生年份班级(班级要用小写字母LOWER函数)
解答:select sname,2006-sage 出生年份 ,lower(sclass) from student
13、 查询全体同学的姓名/出生年份/所在班级列为YearOfBirth
解答:select sname,2006-sage YearOfBirth ,sclass from student
14、 查询选课中学员的学号并且去掉重复行用distinct
解答:select distinct sno from sc
15、 查Y02班全体同学名单
解答:select sno from student where sclass='y02'
16、 查所有年龄在20岁以下的同学姓名及年龄
解答:select sname,sage from student where sage<20
17、 查考试不合格的同学学号
解答:select sno from sc where grade<60
18、 查年龄在19-20岁(包括19-20)之间的同学姓名班级年龄
解答:select sname,sclass,sage from student where sage>=19 and sage<=20
19、 查年龄不在19-20岁之间的同学的姓名,班级,年龄
解答:select sname,sclass,sage from student
where sage not between 19 and 20
20、 查Y02班和Y05班的同学姓名,性别
解答:select sname ,ssex from student
where sclass='y02' or sclass='y05'
21、 查不是Y02班和Y05班的同学的姓名,性别
解答:select sname,ssex from student where
not sclass='y02' and not sclass='y05'
22、 查所有姓刘的同学的姓名,学号,性别
解答:select sname,sno,ssex from student where sname like '刘%'
23、 查所有姓张且全名为2个汉字的学生姓名
解答:select sname,sno,ssex from student where sname like '张_'
24、 某些学生未考试查缺少成绩的同学的学号和课程号
解答:select sno,cno from sc where grade is null
25、 查所有成绩的同学的学号,课程号,和成绩
解答:select sno,cno from sc where grade is not null
26、 查Y02班年龄在20岁以下的姓名和年龄
解答:select sname,sage from student where sclass='y02' and sage<20
27、 查选修1号课程的同学的学号和成绩,按降序排列
解答:select sno,grade from sc where cno=1 order by grade desc
28、 查全体同学信息查询结果按所在班级的班级名称按降序排列,同班同学按年龄升序排列
解答:select *from student order by sclass desc , sage asc
29、 查学员总人数
解答:select count(*) from student
30、 查选修课程学员人数
解答:select count(*) from sc
31、 统计1号课的学员平均成绩
解答:select avg(grade) from SC where cno=1
32、 查选修1号课和同学最高成绩
解答:select max(grade) from SC where cno=1
33、 求各个课程号及相应选课人数
解答:select count(*) from SC group by cno
34、 查选取1门以上课程的同学学号
解答:select sno from SC group by sno having count(cno)>1
35、 查每个学员及其选修,课程情况
解答:select sno,cno from SC
36、 查每个学员及其选修课程情况对没有选课的也要输出
其姓名,学号,性别,班级
解答:select a.sno,a.sname,a.sage,a.ssex,a.sclass,sc.cno,sc.grade
from student a left outer join sc on a.sno=sc.sno order by a.sname
37、 查选取2号课程且成绩在90分以上同学
解答:select *from SC where cno=2 and grade>9
38、 查询每个同学学号姓名,选课程名称及成绩
解答:select a.sno,a.sname,sc.cno,sc.grade from
student a left outer join SC on a.sno=SC.sno order by SC.sno
39、 查与刘晨在一个班的同学
解答:select sname from student where sclass in
(select sclass from student where sname='刘晨')
40、 选取C语言的同学学号和姓名
解答:select sno,sname from student where sno in (select sno from SC
where cno in (select cno from course where cname='C语言'))
41、 查其他班中比Y02班某一同学大的同学姓名和年龄
解答:select sname,sage from student where sage>any
(select sage from student where sclass='Y02') and sclass<>'Y02'
42、 查其他班中比Y02班同学全部都大的同学姓名和年龄
解答:select sname,sage from student where sage>all
(select sage from student where sclass='Y02') and sclass<>'Y02'
43、 查选取1号课程的学员的姓名
解答:select sname from student where sno in
(select sno from SC where cno=1)
44、 查没有选取1号课程的学员的姓名
解答:select sname from student where sno not in
(select sno from SC where cno =1)
45、 查Y02班同学及年龄不大于19岁的学员(union)
解答:select *from student where sclass ='y02' union
select *from student where sage<=19
46、 查询选取1号课程或者2号课程的同学学号
解答:select distinct sno from SC where cno=1 or cno=2
47、 将4号学员的年龄改为23岁
解答:update student set sage=23 where sno=4
48、 将所有同学的年龄增加1岁
解答:update student set sage=sage+1
49、 Y02班的同学的成绩改为100分
解答:update sc set grade=100 where sno in
(select sno from student where sclass='y02')
50、 删除学号为1的同学记录
解答:delete from student where sno=1