实验六:数据表综合查询
四、实验内容
以数据库原理实验4数据为基础,请使用T-SQL 语句实现进行以下操作:
1. 查询名字中第2个字为‘向’的学生姓名和学号及选修的课程号、课程名;
select a.sname, a.sno,b.cno,b.cname from student a,course b,sc c where a.sname like '_向%' and a.sno=c.sno and b.cno=c.Cno
2. 列出选修了‘数学’或者‘大学英语’的学生学号、姓名、所在院系、选修
课程号及成绩;
Select a.sno,sname,sdept,c.Cno,grade from student a, course b,sc c Where b.cname in('数学','大学英语') and a.sno=c.sno and b.cno=c.Cno
3. 查询缺少成绩的所有学生的详细情况;
Select * from student
Where sno in(select sno from sc where grade is null)
4. 查询与‘张力’(假设姓名唯一)年龄不同的所有学生的信息;
Select * from student
Where sage <>(select sage from student where sname='张力')
5. 按照?学号,姓名,所在院系,已修学分?的顺序列出学生学分的获得情况。
其中已修学分为考试已经及格的课程学分之和;
Select a.sno,sname,sdept,sum(ccredit) as 已修学分 from student a,course b,sc c
Where grade>60 and a.sno=c.sno and b.cno=c.Cno group by a.sno,sname,sdept
6. 查找选修了至少一门和张力选修课程一样的学生的学号、姓名及课程号;
Select a.sno,a.sname,c.Cno from student a,sc c
where Cno in (select Cno from sc where sno in(select sno from student where sname='张力')) and a.sno=c.sno
7. 查询只被一名学生选修的课程的课程号、课程名;
select cno,cname from course
where cno in(select cno from sc group by cno having COUNT(*)=1)
8. 使用嵌套查询出选修了?数据结构?课程的学生学号和姓名;
select sno,sname from student
where sno in(select sno from sc where Cno=(select Cno from course where cname='数据结构'))
9. 使用嵌套查询查询其它系中年龄小于CS系的某个学生的学生姓名、年龄和
院系;
select sname,sage,sdept from student
where sage
and sdept<>'cs'
10. 使用ANY、ALL 查询,列出其他院系中比WM系所有学生年龄小的学生的姓名;
select sname from student
where sage
and sdept<>'wm'
11. 分别使用连接查询和嵌套查询,列出与‘张力’在一个院系的学生的信息;
select b.* from student a,student b
where a.sname='张力' and a.sdept=b.sdept 或
SELECT * FROM Student
WHERE Sdept IN(SELECT Sdept FROM Student WHERE sname= '张力')
12. 使用集合查询列出CS系的学生以及性别为女的学生学号及姓名;
SELECT sno,sname from student where sdept='cs' union
select sno,sname from student
where ssex='女'
13. 使用集合查询列出CS系的学生与年龄不大于19岁的学生的交集、差集;
SELECT * FROM Student WHERE Sdept='CS' EXCEPT SELECT * FROM Student WHERE Sage <=19
SELECT * FROM Student WHERE Sdept='CS' INTERSECT SELECT * FROM Student WHERE Sage <=19
14. 使用集合查询列出选修课程1的学生学号集合与选修课程2的学生学号集合
的交集;
SELECT Sno FROM SC WHERE Cno='1' Intersect
SELECT Sno FROM SC WHERE Cno= '2'