SQL 练习题自写答案

--1
create database edu;

--2
use edu;
create schema zhangjie authorization guest;

--3
create table student(
	sno char(8) not null,
	sname char(8) not null,
	sex char(2),
	native char(20),
	birthday datetime,
	pno char(4),
	dno char(6),
	classno char(4),
	entime datetime,
	home varchar(40),
	tel varchar(40),
	primary key(sno)
);

create table course(
	cno char(10) not null,
	cname char(20) not null,
	cpno char(10),
	experiment tinyint,
	lecture tinyint,
	semester tinyint,
	credit tinyint
	primary key(cno)
	);

create table student_course(
	sno char(8) not null,
	cno char(10) not null,
	score tinyint,
	primary key(sno,cno),
	foreign key(sno) references student(sno),
	foreign key(cno) references course(cno)
);

create table teacher(
	tno char(8) not null,
	tname char(8) not null,
	sex char(2),
	birthday datetime,
	dno char(6),
	pno varchar(20),
	home varchar(40),
	zipcode char(6),
	tel varchar(40),
	email varchar(40),
	primary key(tno)
	);
	
create table teacher_course(
	tcid smallint not null,
	tno char(8),
	classno char(4),
	cno char(10),
	semester char(6),
	schoolyear char(10),
	classtime varchar(40),
	classroom varchar(40),
	weektime tinyint
	primary key(tcid),
	foreign key(tno) references teacher(tno),
	foreign key(cno) references course(cno)
);

create table department(
	dno char(6) not null,
	dname char(8) not null,
	dhome varchar(40),
	dzipcode char(6),
	dtel varchar(40),
	primary key(dno)
	);
--4
create unique index upup on student(sname desc); 

create index upupup on student(birthday asc);

create index aaa on student_course(sno asc,cno desc);

create index bbb on teacher(tno asc);

drop index student_course.aaa;
drop index student.upup;
--5
insert into student
values('20101001','张三','男','河北沧州','1989-03-15','2','1','2','2010-9-1','河北省沧州市',null),

('20101002','张强','男','河北邢台','1990-05-01','2','1','2','2010-9-1','河北省邢台市','13145322345'),

('20101003','张小兵','男','山东济南','1989-03-15','1','2','4','2010-9-1','河北省沧州市',null),

('20101004','李燕','男','山东济南','1989-03-15','1','1','2','2010-9-1','山东济南','15194598762'),

('20101005','李晓','男','山东德州','1989-03-15','2','2','2','2010-9-1','山东德州',null),

('20101006','上官青','女','陕西西安','1989-03-15','3','4','2','2010-9-1','陕西西安','15192598762'),

('20101007','李晓芳','女','陕西西安','1989-03-15','3','3','3','2010-9-1','陕西西安',null),

('20101008','上官文宏','女','北京市','1989-03-15','2','2','2','2010-9-1','北京市','15194598232'),

('20101009','上官文宏','女','河北保定','1989-03-15','1','1','2','2010-9-1','河北保定',null),

('20101010','张倩','女','河北保定','1989-03-15','2','3','2','2010-9-1','河北保定','15123298762'),

('20101011','刘英伟','男','河北保定','1989-03-15','3','2','2','2010-9-1','河北保定',null),

('20101012','刘伟','男','河北保定	','1989-03-15','2','4','2','2010-9-1','河北保定','15232598762'),

('20101013','翟南','男','上海市','1989-03-15','2','1','3','2010-9-1','上海市','15194598772'),

('20101014','窦士厝','男','云南昆明','1989-03-15','3','4','1','2010-9-1','云南昆明','15193248762');				 			 		



insert into course
values('1','数据库系统概论','3','20','72','1','3'),
('2','100%掌握财务报表','3','30','54','1','2'),
('3','数据结构','5','30','72','1','4'),
('4','c++程序设计','5','15','72','1','4'),
('5','c 语言程序设计',null,'18','45','1','3'),
('6','WEB 开发',null,'28','72','1','2'),
('7','计算机导论',null,'10','32','1','1');

insert into student_course
values('20101001','1',70),
('20101001','2',95),
('20101001','3',90),
('20101001','4',88),
('20101001','5',null),
('20101001','6',null),
('20101002','1',90),
('20101002','2',40),
('20101002','3',90),
('20101003','1',90),
('20101004','2',50),
('20101004','3',90),
('20101005','1',20),
('20101005','2',80),
('20101006','3',90),
('20101007','3',60),
('20101008','3',null),
('20101009','3',11),
('20101010','3',58),
('20101011','3',45);

insert into teacher
values('836001','董青','男','1975-04-26','1','讲师','河北省石家庄市','50000','13798789765','dong@gmail.com'),
('836002','李亮','男','1975-04-26','1','副教授','河北省石家庄市','834000','13728789765','liliang@gmail.com'),
('836003','李晓亮','男','1975-04-26','1','讲师','河北省石家庄市','50300','12228789765','dong@gmail.com'),
('836004','项天','男','1975-04-26','2','教授','河北省石家庄市','832000','13798389765','dong@gmail.com'),
('836005','余孝天','男','1975-04-26','2','讲师','河北省石家庄市','50300','13798789765','dong@gmail.com'),
('836006','鲁婵娟','女','1975-04-26','3','副教授','河北省石家庄市','830200','13733789765','dong@gmail.com'),
('836007','李冉冉','女','1975-04-26','3','讲师','河北省石家庄市','833300','13727822765','dong@gmail.com'),
('836008','庄文','男','1975-04-26','2','教授','新疆自治区乌鲁木齐市','8200','13333789765','dong@gmail.com');

insert into teacher_course
values('1','836001','1','1','1','2010','8:00-11:00','公教楼305','3'),
('2','836002','1','2','1','2010','8:00-11:00','公教楼305','3'),
('3','836002','2','2','1','2010','8:00-11:00','c305','3'),
('4','836003','2','3','1','2010','8:00-11:00','c305','3'),
('5','836003','2','1','1','2010','8:00-11:00','v305','3'),
('6','836003','1','3','1','2010','8:00-11:00','v305','3'),
('7','836004','3','4','1','2010','8:00-11:00','b305','3');

insert into department
values('1','法政学院','河北省石家庄市裕华区南二环东路 20 号','50024','80788100'),
('2','教育学院','河北省石家庄市裕华区南二环东路 20 号','50024','80788101'),
('3','软件学院','河北省石家庄市裕华区南二环东路 20 号','50024','80788102'),
('4','数信学院','河北省石家庄市裕华区南二环东路 20 号','50024','80788102');

--6.查询
--1)查询全体学生的学号、姓名、籍贯
select sno,sname,native
from student;

--2)查询所有课程的名称及学分
select cname,credit
from course;

--3)查询教师的姓名、性别、联系电话
select tname,sex,tel
from teacher;

--4)查询每门课程的课程编号、课程名称及理论学时
select cno,cname,lecture
from course;

--5)查询全体学生的姓名、联系电话,并在前面加上字符串‘联系方式’
select sname, '联系方式 :' +tel '联系方式'
from student;

--6)查询全体教师的人数
select count(distinct tno)
from teacher;

--7)查询全体教师的姓名、家庭住址、邮政编码(使用列别名)
select tname,home,zipcode'邮政编码'
from teacher;

--8)查询全体学生的姓名
select sname
from student;

--9)查询本学期有课的教师编号
select tno
from teacher
where semester = 1 and tno in (select tno 
											from teacher_course);

--10)查询所有选课的学生学号
select sno
from student
where sno in (select sno 
						from student_course);

--11)查询籍贯为河北保定的学生的学号和姓名
select sno,sname
from student 
where native = '河北保定';

--12)查询课程学分小于 3 分的课程名称
select cname
from course
where credit < 3;

--13)查询在 c201 教室上课的教师编号
select distinct tno
from teacher_course
where classroom = 'c201';

--14)查询软件学院的办公电话
select dtel
from department
where dname = '软件学院';

--15)查询学生成绩在 60-90 分的学生的学号和课程号
select sno,cno
from student_course
where score between 60 and 90;

--16)查询学分为 3 分的课程信息
select *
from course
where  credit = '3';

--17)查询在教学楼 C 座上课的教师的编号
select tno
from teacher_course
where classroom like '%c%';

--18)查询有先行课的课程编号及课程名
select cno,cname
from course
where cpno is not null;

--19)查询籍贯为山东省的学生基本信息
select *
from student
where native like '山东%';

--20)查询姓名为 3 个字,前两个字为‘上官’的学生学号和姓名
 select sno,sname
from student
where sname like '上官__';
 
--21)查询专业号为 3 的女学生的姓名、生日和家庭住址
select sname,birthday,home
from student
where pno = '3' and sex = '女';

--22)查询学号为‘20101001’的学生选修的课程号及成绩,结果按成绩升序排
--列
select cno,score
from student_course
where sno = '20101001'
order by score asc;

--23)查询所有课程的基本信息,结果按授课学时降序排列,学时相同按学分升
--序排列
select *
from course 
order by lecture desc,credit asc;

--24)查询所有男教师的姓名和出生日期,结果年龄从小到大排序
select tname,birthday
from teacher
order by birthday desc;

--25)查询选修 2 号课程学生成绩的最大值和最小值 
select MAX(score),MIN(score)
from student_course
where cno = '2';

--26)查询所有教师的人数
select COUNT(tno)
from teacher;

--27)查询所有专业号为 1 的学生人数
select COUNT(sno)
from student
where pno = 1;

--28)查询选课人数超过 5 人的课程编号
select cno
from student_course
group by cno
having COUNT(*)>5;

--29)查询所有学生的基本信息以及所在学院的名称和学院联系电话
select student.*,department.dname,department.dtel
from student,department
where student.dno = department.dno;

--30)查询教师李亮的上课时间、上课地点和每周学时数
select teacher_course.classno,classroom,weektime
from teacher_course,teacher
where teacher_course.tno = teacher.tno and teacher.tname = '李亮';

--31)查询选修了数据库系统概论或数据结构的学生的学号和姓名
select sno,sname
from student
where sno in (select sno 
						from student_course
							where 	cno in (select cno 
													from course
													where cname = '数据库系统概论' or cname = '数据结构'));

--32-67 要求至少使用两种方式查询(对复杂查询,可以建立视图)

---------------------------------------------------------------------------32)查询至多有两名男生的班级
select classno
from student
where sex = '男'
group by classno
having COUNT(*)<2
union
select classno
from student
where classno not in (select distinct classno 
								from student
								where sex = '男');
								
--33)查询至少有一名教授的学院的信息
select distinct department.*
from department,teacher
where department.dno = teacher.dno and teacher.pno = '教授';

select *
from department
where dno in (select distinct dno
						from teacher
						 where pno = '教授');

-----------------------------------------------------------34).查询出每个老师及其教课情况(提到每个用左右外连接)
select teacher.*,teacher_course.*
from teacher left outer join teacher_course
on teacher.tno = teacher_course.tno;

-----------------------------------------------------------35).查询每门课程及其被选修的人数 group by后可以是多列

select course.cno,course.cname,COUNT(*)
from course left outer join student_course
on course.cno = student_course.cno
group by course.cno,course.cname;

--36).查询籍贯是河北省的教师所教的课程信息
select *
from course
where cno in(select cno
						from  teacher_course
						where tno in (select tno 
												from teacher
												where 	home like '河北%'));

--37).查询软件学院学生情况
select *
from student
where dno in (select dno 
						from department
						where 	dname = '软件学院');

-----------------------------------------------------------------38).查询班级人数最多的班的学生情况	(top 1)
--建议					
select *
from student
where classno in (select classno
							from student
							group by classno
							having COUNT(*) >= all (select COUNT(*)
																	from student
																	group by classno));

select *
from student
where classno in (select top 1 classno
							from student
							group by classno 
							order by COUNT(*) desc );



--39).查询张姓学生选修的课程号、课程名

select cno,cname
from course
where cno in (select distinct  cno
						from student_course
							where	sno in (select sno	
													from student
													where sname like '张%'));

--40).查询男学生选修课程的平均分
select cno,AVG(score)
from student_course
where sno in (select sno
						from student	
						where sex = '男'	)
group by cno;	

--41).查询哪些学生选修了授课学时为 54 的课程
select *
from student
where sno in (select distinct sno
						from student_course
							where 	cno in (select cno
													from course
													where lecture = 54));


select *
from student
where not exists(select *
							from course
							where lecture = 54  and not exists (select *
																					from student_course
																					where student.sno = student_course.sno
																					and course.cno = student_course.cno));



-----------------------------------------------------------------42).查询比软件学院学生年龄都小的其他系学生的信息。YEAR(birthday)取年

select *
from student
where YEAR(birthday) > all(select YEAR(birthday)
											from student
											where dno in (select dno
																	from department
																		where 	dname = '软件学院'));

--------------------------------------------------------43).查询比数信学院学院学生年龄大的教育学院学生信息。
select *
from student
where YEAR(birthday) < any(select YEAR(birthday)
										from student
										where dno in (select dno
																from department
																where dname = '数信学院')) and dno in(select dno 
																																from department
																																where dname = '教育学院');

--44).查询班级号为 1 的班的学生 c 语言程序设计成绩的平均成绩
select AVG(score)
from student_course
where sno in (select sno
						from student
							where classno = '1'	) and cno in (select cno 
																					from course
																					where cname = 'c 语言程序设计');

--------------------------------------45).查询计算机导论平均成绩最高的班级。
select 	classno,AVG(score)
from student_course,student
where student_course.sno = student.sno and cno in (select cno
																					from course
																					where cname = '计算机导论'	)		
group by classno
having AVG(score) >= all (select 	AVG(score)
											from student_course,student
											where student_course.sno = student.sno and cno in (select cno
																																from course
																																where cname = '计算机导论'	)		
											group by classno);
																														

----------------------------------------46).查询选修人数最多的课程是哪个老师教的,显示教师号,教师姓名
select *
from teacher
where tno in(select tno
					from teacher_course
					where cno in (select cno
					from student_course
					group by cno
					having COUNT(*) >=all(select COUNT(sno)
														from student_course
														group by cno)));

--------------------------------------47).查询余孝天老师所教各门课程的平均成绩
select cno ,AVG(score)
from student_course
where cno in (select cno
						from teacher_course
						where tno = (select tno 
											from teacher
											where tname = '余孝天'))
group by cno;


------------------------------------------------48).查询鲁婵娟老师所教课程的各个班级平均成绩
select classno,avg(score) '平均成绩'
from student_course,student
where student_course.sno = student.sno and cno in((select cno 
																					from teacher_course  
																						where	tno in(select tno 
																												from teacher
																												where tname = '鲁婵娟')))
group by classno;

													

--49).查询鲁婵娟老师所教课程的学生的成绩
select *
from student_course,student
where student_course.sno = student.sno and cno in((select cno 
																					from teacher_course  
																						where	tno in(select tno 
																												from teacher
																												where tname = '鲁婵娟')))

--50).查询有多少人选修了《数据结构》课程的先修课。
select COUNT(distinct sno)
from student_course
where cno in (select cpno 
						from course 
							where 	cname = '数据结构');

--51).查询软件学院教师所教课程信息
select *
from course
where cno in (select cno 
						from teacher_course
								where tno in(select tno 
														from teacher
															where dno in (select dno
																					from department
																						where dname = '软件学院')));
--52).查询软件学院教师所教课程的成绩,将结果按课程号降序排列。

select *
from course
where cno in (select cno 
						from teacher_course
								where tno in(select tno 
														from teacher
															where dno in (select dno
																					from department
																						where dname = '软件学院')))
order by cno desc;

--53).查询未授课教师的姓名和系
select tname,dno
from teacher
where  tno not in (select	tno
							from teacher_course );

--54).按职称显示软件学院的教师人数。
select pno,COUNT(tno)
from teacher
where dno in (select dno 
						from department
						where 	dname = '软件学院')
group by pno;

-----------------------------------------------55).查询成绩高于《数据结构》平均成绩的学生信息。
select *
from student
where sno in (select sno
						from student_course
						where cno = (select cno
												from course
												where cname = '数据结构'	)
												and score >(select AVG(score)
																	from student_course
																	where cno in (select cno 
																							from course
																							where cname = '数据结构')));
--56).查询学生选修课程的情况,显示学号、姓名、教师姓名、课程名称、成绩。
select student_course.sno,student.sname,cname,score,teacher.tname
from student_course,student,course,teacher_course,teacher
where student_course.sno = student.sno and course.cno = student_course.cno  and teacher_course.cno = course.cno and teacher_course.tno = teacher.tno;

--57).查询法政学院教师第一学期所带班级 
select classno
from teacher_course
where semester = '1' and tno in (select tno
														from teacher
														where dno in(select dno
																				from department
																				where dname = '法政学院'))

--58).查询第 2 学期哪些教师在公教楼上课。
select *
from teacher
where tno in (select tno
						from teacher_course
						where semester = '1' and classroom like '公教楼%');


--59).查询数信学院的学生各科平均成绩,显示课程名、平均成绩。

select cno,AVG(score)
from student_course
where cno in (select cno
						from teacher_course
							where tno in (select tno
													from teacher
														where dno in ((select dno
																					from department
																					where dname = '数信学院'))))
group by cno;


--60).查询选修了全部课程的学生的信息。
--没有一门课没有不选

select *
from student
where not exists (select *
							from course
							where not exists(select *
														from student_course
														where student.sno = student_course.sno
														and course.cno = student_course.cno));

--61).查询至少选修了鲁婵娟所教的课程的学生信息
select *
from student
where not exists (select *
							from course
							where cno in (select cno
												from teacher_course
												where tno in	(select tno
																	from teacher
																	where tname = '鲁婵娟'	)) and not exists(select *
																																	from student_course
																																	where student.sno = student_course.sno
																																	and course.cno = student_course.cno));

--62).查询和张小兵同一个班级的其他学生的信息.
select *
from student
where classno in (select classno
							from student
								where sname = '张小兵')
								and sname != '张小兵';

--63).查询和刘英伟同年出生的学生信息(参考 sql 日期操作函数)
select *
from student
where YEAR(birthday) in (select YEAR(birthday)
										from student
											where sname = '刘英伟')
											and sname != '刘英伟';

--64).查询选修了 3 门以上课程学生信息
select *
from student
where sno in (select sno
					from student_course
					group by sno
					having COUNT(*) > 3);


--65).查询至少有 2 名女生的班级的学生信息
select *
from student
where classno in (select classno
								from student
								where sex = '女'
								group by classno
								having COUNT(*)>2);

--66).查询软件学院年龄小于 25 岁的老师的信息
select *
from teacher
where 	dno in (select dno	
						from department
						where dname = '软件学院')  and 	2020-YEAR(birthday) < 25; 

--67).查询低于该门课程平均分的学生的成绩信息。
select *
from student_course a
where score > (select AVG(score)
						from student_course b
						where a.cno = b.cno	
						group by b.cno);


 








--集合查询
--68).年龄小于 23 岁的女老师和女学生的信息
select *
from teacher
where 2020 - YEAR(birthday) <23
--union
select *
from student
where sex = '女';

--69).查询不教课的老师信息。
select *
from teacher
where tno not in(select tno
							from teacher_course
							);

--70).查询班级号为 3,且性别为女的学生的信息
select *
from student
where classno = '3' and sex = '女';

--7.更新
--(1)将年龄小于 18 岁的男同学的年龄都增大 1 岁。
update student
set birthday =  birthday -1
where 2020- YEAR(birthday) >18;

--(2)将王英老师的联系电话改为 83421236。
update teacher
set tel = '83421236'
where tname = '王英';


--(3)将“数据结构”课程的上课地点改为“D403”。
update teacher_course
set classroom = 'D404'
where cno in (select cno
						from course
							where cname = '数据结构');

--(4)将“数据库原理”课程成绩在 70 分以下的加 5 分。
update student_course
set score = score+5
where cno in(select cno
						from course
							where cname = '数据库原理');

--(5)将所有计算机系学生的“高等数学”课的成绩加 10 分。
update student_course
set score = score + 10
where cno in (select cno
						from course
							where cname = '高等数学') and sno in (select sno
																								from student
																									where dno in (select dno
																															from department
																																where dname = '计算机系'));
--(6)将所有数学系学生的联系电话删除。
update student
set tel = null
where dno in (select dno 
						from department
							where dname = '数学系'	);

--(7)删除没有成绩的选课记录。
delete 
from student_course
where score is null;

--(8)删除学生中姓名有“强”的记录。(有错误)
delete
from student
where sname like '%强%';

--(9)删除数学系女同学的记录。
delete
from student
where sex = '女' and dno in (select dno
												from department
													where dname = '数学系');

--(10)将 2000 年 9 月 1 日以前入学的学生记录删除。
delete 
from student
where entime <2000-9-1;

--(11)删除计算机系所有教师的上课记录。
delete
from teacher_course
where tno in (select tno 
						from teacher
							where dno in (select dno 
													from department
														where dname = '计算机系'));

------------------------------------(12)将平均成绩最高的课程学分加 1。
update course
set credit = credit +1
where cno in (select cno
						from student_course
								)


--(13)将实验时数为 36 的课程的成绩加上 10%。
update student_course
set score =score + score*0.1
where cno in (select cno
						from course
						where 	experiment = '36');
						
--(14)将所有没有上课的教师记录删除。
delete 
from teacher
where tno not in (select tno 
							from teacher_course);

--8.视图
--(1)将女教师的基本情况定义为视图。
create view woman
as
select *
from teacher
where sex = '女';
--(2)将数学系班级号为 2008 的学生的学号、姓名、性别和专业号定义为视图。
create view bbb
as
select sno,sname,sex,pno
from student
where classno='2008' and dno in (select dno 
														from department
														where dname = '数学系');

--(3)将各个学院的学生人数定义为视图,包括学院编号、人数
create view ccc(cno,人数)
as
select dno,COUNT(distinct sno)
from student
group by dno;

------------------------------------(4)将平均成绩大于 90 分的课程定义为视图,包括课程号、课程名和平均成
--绩。
create view ddd(cno,cname,平均成绩)
as
select course.cno,course.cname,AVG(score)
from student_course,course
where student_course.cno = course.cno
group by course.cno,course.cname
having AVG(score) >90;

select *
from ddd;

--(5)将各个教师所教课程的选课人数定义为视图,包括教师编号、选课人数。
create view eee
as
select cno,COUNT(sno)
from student_course
group by cno;


--(6)查询人数超过 500 人的学院名称。
select dname
from department
where dno in (select dno
							from student
							group by dno 
							having COUNT(*) >4);

--(7)查询选修王强老师所教课程的人数。
create view ggg(cno,人数)
as
select cno,COUNT(*)
from teacher_course
where tno in (select tno
						from teacher
						where tname = '王强'	)
group by cno;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值