数据查询(综合篇)-------实验六

数据查询(综合篇)-------实验六_第1张图片数据查询(综合篇)-------实验六_第2张图片四、实验内容
1.查询每个学生及其选课情况(等值连接、右连接)
2.查询每门课的间接选修课
3.查询既选修了2号课程又选修了3号课程的学生姓名、学号(连接查询、不相关子查询、相关子查询)
连接查询
4.查询和刘晨同一年龄的学生(自身连接、不相关子查询、相关子查询)
5.查询选修了课程名为“数据库”的学生姓名和年龄(不相关子查询、相关子查询)
6.查询其他系中比IS系任一学生年龄小的学生名单(两种解法)
7.查询其他系中比IS系所有学生年龄都小的学生名单(两种解法)
8.查询选修了全部课程的学生姓名(不相关子查询、相关子查询)
9.查询计算机系学生及其性别是男的学生(多条件查询、相关子查询)
10.查询张立同学不学的课程的课程号(不相关子查询、相关子查询)
11.查询选修了3号课程的学生平均年龄(连接查询、不相关子查询)
12.求每门课程学生的平均成绩
13.统计每门课程的学生选修人数(超过1人的才统计)。要求输出课程号和选修人数,结果按人数降序排列,若人数相同,按课程号升序排列

create table student1(sno char(8) primary key,sname char(8),ssex char(2) constraint ck_student_ssex1 check(ssex='男' or ssex='女'),sage int,sdept char(10))
create table course1(cno char(2) primary key,cname char(30),credit int,cpno char(3))
create table sc1(sno char(8),cno char(2),grade int constraint ck_sc_grade1 check(grade>0 and grade<100),primary key(sno,cno),foreign key (sno) references student1(sno),foreign key (cno) references course1(cno))
insert into student1(sno,sname,ssex,sage,sdept)
values('95001','李勇','男','20','CS')
insert into student1(sno,sname,ssex,sage,sdept)
values('95002','刘晨','女','19','IS')
insert into student1(sno,sname,ssex,sage,sdept)
values('95003','王敏','女','18','MA')
insert into student1(sno,sname,ssex,sage,sdept)
values('95004','张立','男','19','IS')
insert into student1(sno,sname,ssex,sage,sdept)
values('95005','刘云云','女','18','CS')
insert into course1(cno,cname,credit,cpno)
values('1','数据库','4','5')
insert into course1(cno,cname,credit,cpno)
values('2','数学','6',NULL)
insert into course1(cno,cname,credit,cpno)
values('3','信息系统','3','1')
insert into sc1(sno,cno,grade)
values('95001','1',92)
insert into sc1(sno,cno,grade)
values('95001','2',85)
insert into sc1(sno,cno,grade)
values('95001','3',88)
insert into sc1(sno,cno,grade)
values('95002','2',90)
insert into sc1(sno,cno,grade)
values('95002','3',80)
insert into sc1(sno,cno,grade)
values('95003','2',85)
insert into sc1(sno,cno,grade)
values('95004','1',58)
insert into sc1(sno,cno,grade)
values('95004','2',85)
--1.查询每个学生及其选课情况(等值连接、右连接)
--等值
select student1.*,sc1.* from student1,sc1 where student1.sno=sc1.sno
--右
select student1.*,cno,grade from sc1 right outer join student1 on(student1.sno=sc1.sno)
--2.查询每门课的间接选修课
select c1.cno,c2.cpno from course1 c1,course1 c2 where c1.cpno=c2.cno
--3.查询既选修了2号课程又选修了3号课程的学生姓名、学号(连接查询、不相关子查询、相关子查询)
--连接查询
select sname,s1.sno from student1 s1,sc1 c1,sc1 c2 where c1.sno=s1.sno and c1.sno=c2.sno and c1.cno='2' and c2.cno='3'
--不相关子查询
select sname,sno from student1 where sno in (select sno from sc1 s1 where s1.cno='2' and s1.sno in (select sno from sc1 s2 where s2.cno='3'))
select sname,student.sno from sc s1,sc s2,student where student.sno=s1.sno and s1.sno=s2.sno and s1.cno='2' and s2.cno='3' 
--相关子查询
select sname,sno from student1 where exists (select * from sc1 s1,sc1 s2 where s1.cno='2' and s2.cno='3'and s1.sno=s2.sno and student1.sno=s1.sno)
select sname,sno from student where exists (select * from sc where student.sno=sc.sno and cno='2') and exists(select * from sc where student.sno=sc.sno and cno='3')
--4.查询和刘晨同一年龄的学生(自身连接、不相关子查询、相关子查询)
--自身连接
select s2.sno,s2.sname,s2.sage from student1 s1,student1 s2 where s1.sname='刘晨' and s1.sage=s2.sage
--不相关子查询
select sno,sname,sage from student1 where sage=(select sage from student1 where sname='刘晨')
--相关子查询
select sno,sname,sage from student1 s1 where exists (select * from student1 s2 where s2.sname='刘晨' and s1.sage=s2.sage)
--5.查询选修了课程名为“数据库”的学生姓名和年龄(不相关子查询、相关子查询)
--不相关子查询
select sname,sage from student1 where sno in(select sno from sc1 where cno=(select cno from course1 where cname='数据库'))
--相关子查询
select sname,sage from student1 where exists(select * from sc1 where student1.sno=sc1.sno and exists (select * from course1 where cname='数据库'and sc1.cno=course1.cno))
--6.查询其他系中比IS系任一学生年龄小的学生名单(两种解法)
select sno,sname from student1 where sdept!='IS' and sage'IS' and sage<(select max(distinct sage) from student1 where sdept='IS')
select sno,sname from student1 s1 where exists (select * from student1 s2 where s1.sdept<>'IS' and s2.sdept='IS' and s1.sage'Is' and sage'IS' and sage<(select min(distinct sage) from student1 where sdept='IS')

--8.查询选修了全部课程的学生姓名(不相关子查询、相关子查询)
--不相关子查询
select sname from student1 where sno in (select sno from sc1 group by sno having count(*)=(select count(*) from course1))
--相关子查询()
select sname from student1 where not exists(select * from course1 where not exists(select * from sc1 where student1.sno=sc1.sno and sc1.cno=course1.cno))
--9.查询计算机系学生及其性别是男的学生(多条件查询、相关子查询)
--多条件查询
select sno,sname,ssex,sdept from student1 where sdept='IS' and ssex='男'
--相关子查询
select s1.* from student1 s1 where exists(select * from student1 s2 where sdept='IS' and ssex='男' and s1.sno=s2.sno)
--10.查询张立同学不学的课程的课程号(不相关子查询、相关子查询)
--不相关子查询
select cno from course1 where cno not in (select cno from sc1 where sno=(select sno from student1 where sname='张立'))
--相关子查询
select cno from course1 where not exists(select * from sc1 where course1.cno=sc1.cno and exists(select * from student1 where sname='张立' and student1.sno=sc1.sno) )
--11.查询选修了3号课程的学生平均年龄(连接查询、不相关子查询)
--连接查询
select avg(sage) junlin from student1,sc1 where cno='3' and student1.sno=sc1.sno
--不相关子查询
select avg(sage) junlin from student1 where sno in (select sno from sc1 where cno='3')
--12.求每门课程学生的平均成绩
 select cno,avg(grade) 平均成绩 from sc group by cno
--13.统计每门课程的学生选修人数(超过1人的才统计)。要求输出课程号和选修人数,结果按人数降序排列,若人数相同,按课程号升序排列
select cno,count(*) 选修人数 from sc1 group by cno order by count(*) desc,cno

你可能感兴趣的:(数据库)