数据库技术_查询练习题

本文涵盖了多个复杂的SQL查询示例,包括:查询与特定学生学分数相等的其他学生,查找低于平均成绩的学生,统计特定年份学生数量与平均成绩,筛选成绩高于王名最高分的同学,找出未注册课程表的学生,选修超8学分的学生,计算指定学生平均成绩,寻找相同系且年龄更大的同学,以及统计选课人数。这些例子展示了SQL在数据筛选和聚合操作中的强大能力。
摘要由CSDN通过智能技术生成

1.查询选修了和王名一样学分数的其他同学姓名。

select sname
from student
where sno in(select sno 
from sc,course 
where sc.cno =course.cno 
group by sno 
having sum(ccredit)=(select sum(ccredit) 
from sc,course 
where sc.cno =course.cno  
and sno in(select sno 
from student 
where sname='王名') )
and sname<>'王名')

2.查询选修了1号课并且成绩比全班1号课平均成绩低的同学学号及成绩。

select sno,grade
from sc
where cno='1' 
and grade<(select avg(grade) 
from sc 
where cno='1')

3.分别按每年统计出生在1988-1990年三年间的学生人数,平均成绩,(学生人数,平均成绩表示)。

select count(student.sno) as 学生人数,avg(grade) as 平均成绩 
from student,sc 
where student.sno=sc.sno 
group by year(csrq) 
having year(csrq)=1988 
or year(csrq)=1989 
or year(csrq)=1990

4.查询个人平均成绩比王名同学成绩的最高成绩还要高的同学的账户名和密码。

select  user1,password1 
from mm 
where sno in(select sno 
from sc 
group by sno 
having avg(grade)>(select max(grade) 
from sc 
where sno in(select sno 
from student 
where sname='王名')))

5.查询选过课的但没有在MM表中注册的同学姓名。

select sname
from student
where sno in(select sno from sc,course where sc.cno=course.cno) 
and sno not in (select sno from mm )

6.查询选修的学分数超过8个学分的同学学号及学分数。

select sname
from student
where sno in(select sno 
from sc,course 
where sc.cno= course.cno 
group by sno 
having sum(ccredit)>8)

7.查询李明,刘晨两位同学的平均成绩,并用学号和平均成绩表示出来。

select sc.sno,avg(grade) as 平均成绩 
from sc,student 
where sc.sno = student.sno 
and sname in('李明','刘晨') 
group by sc.sno

8.查询和李明同一个系,年龄比他大的同学姓名(当前年2010)。

select sname 
 from student 
 where sdept in(select sdept 
 from student 
 where sname='李明') 
 and 2010-year(csrq)>all(select 2010-year(csrq) 
 from student 
 where sname='李明')

9.查询数据库和数学这两门课的选课人数,并用课程号和选课人数表示出来。

select course.cno as 课程号, count(sc.sno) as 选课人数  
from sc,course 
where sc.cno = course.cno 
and (cname = '数据库' or cname = '数学') 
group by course.cno

10.查询同时选修了1,2,3号课的同学的姓名及系。

select sname,sdept
from student
where sno in (select sno 
from sc 
where sno in(select sno 
from sc 
where sno in(select sno 
from sc 
where cno='1'  ) 
and cno='2') 
and cno='3')
  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值