SQL Server2012——使用SQL语言进行复杂查询

使用SQL语言进行复杂查询

查询每个学生及其选课情况

select student.sno,sname,ssex,sage,sdept,cno,grade
from student,sc
where student.sno=sc.sno

查询每门课的间接先修课

select first.cno,second.cpno
from course first,course second
where first.cpno=second.cno

将STUDENT,SC进行右连接

select student.sno,sname,ssex,sage,sdept,cno,grade
from student left outer join sc on student.sno=sc.sno

查询既选修了2号课程又选修了3号课程的学生姓名、学号

select student.sno,sname
from student inner join sc on student.sno=sc.sno
where cno='3' and sc.sno in
(select sno
from sc
where cno='2')

查询和刘晨同一年龄的学生

select student.sno,sname
from student
where sname!='刘晨' and sage=
(select sage 
from student
where sname='刘晨')

选修了课程名为“数据库”的学生姓名和年龄

select sname,sage
from student
where sno in
(select sno
from sc
where cno in
(select cno
from course 
where cname='数据库'))

查询其他系比IS系任一学生年龄小的学生名单

select student.sno,sname
from student
where sdept<>'IS' and
sage<all
(select sage 
from student
where sdept='IS')

查询其他系中比IS系所有学生年龄都小的学生名单

select student.sno,sname
from student
where sdept<>'IS' and 
sage<any
(select sage 
from student 
where sdept='IS')

查询选修了全部课程的学生姓名

select sname
from student
where Sno in
(select Sno from SC
group by Sno
having count(*) = (select count(*) from course ))

查询计算机系学生及其性别是男的学生

select student.sno,sname
from student
where sdept='IS' and ssex='男'

查询选修课程1的学生集合和选修2号课程学生集合的差集

select sno
from sc 
where cno='1' except 
select sno
from sc
where cno='2'

查询李勇同学不学的课程的课程号

select cno
from course
where cno not in
(select cno
from sc
where sno in
(select sno
from student
where sname='李勇'))

查询选修了3号课程的学生平均年龄

select AVG(sage) as avgsage
from student inner join sc on student.sno=sc.sno
where cno='3'

求每门课程学生的平均成绩

select cno,AVG(grade) as avggrade
from sc
group by cno

统计每门课程的学生选修人数(超过3人的才统计)。要求输出课程号和选修人数,结果按人数降序排列,若人数相同,按课程号升序排列

select course.cno '课程号', count(sc.sno) '人数'
from course,sc 
where course.cno=sc.cno 
group by course.cno having count(sc.sno)>3 order by COUNT(sc.sno) desc,course.cno asc

查询学号比刘晨大,而年龄比他小的学生姓名

select sname
from student
where sno>
(select sno from student where sname='刘晨')and
sage<(select sage from student where sname='刘晨')

求年龄大于所有女同学年龄的男同学姓名和年龄

select sname,sage
from student
where ssex='男'and sage>
(select MAX(sage) from student where ssex='女')
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值