二十条sql语句,您都能写对吗?

haonanernet 2004-06-12 08:31:13
有四个表
(1)学生表(学生号,姓名,性别,年龄,所在班级)
student(sno,sname,sex,age,class)

(2)课程表(课程号,课程名,教师号)
course(cno,cname,tno)

(3)教师表(教师号,教师名,性别,年龄,级别,系)
teacher(tno,tname,sex,age,prof,depart)

(4)分数表(学生号,课程号,分数)
score(sno,cno,grade)

试用sql语句写出下面的查询
(1)查询选修课程号"3-105"且成绩在60-80之间的所有记录

(2)查询选修课成绩为85,86或88的成绩记录

(3)查询'95031'班的学生人数

(4)查询至少有5名学生选修的并以'3'开头的课程的平均成绩

(5)查询最底分大于70,最高分小于90的学生学号

(6)查询'95033'班学生所选课程的平均分

(7)查询选修'3-105'课程的成绩高于'109'号同学成绩的所有学生学号

(8)查询与学号为'108'的同学同岁的所有学生的sno,sname和age

(9)查询"张飞"老师任课的课程号,选修其课程的学生学号和成绩

(10)查询某课程的学生人数多与5人的教师姓名

(11)查询"计算机系"与"电子工程系"的教师姓名与职称

(12)查询选修编号为"3-105"课程且成绩至少高于选修编号为"3-245"的同学的
cno,sno,grade并按成绩从高到低排列

(13)列出所有同学和教师的"姓名",sex,age

(14)查询成绩比该课程平均成绩低的学生的学号,成绩

(15)列出至少两名男生的班号

(16)查询不姓"王"的学生姓名

(17)查询每门课最高分学生的sno,cno,grade

(18)查询与"李朋"同性别并同班的学生姓名

(19)查询"男"教师姓名及其讲授的课程名

(20)查询选修"计算机导论"课程的"男"同学的成绩表

我花了半个小时敲进去的,希望大家能看看
在学习数据库之前,我做过项目,用的sql2000,也写过很多sql语句
但是上面是我们这学期的期末考前练习题,我写错了好几道
所以拿出来和大家一起分享,过几天公布答案,
看看大家谁最先做到百分百正确:)
...全文
418 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
8536105 2004-06-14
  • 打赏
  • 举报
回复
加个问题
如何查询每门课前几名学生?
jackjingsg 2004-06-14
  • 打赏
  • 举报
回复
学着写一下
skyboy0720 2004-06-13
  • 打赏
  • 举报
回复
晕!
newboy3205579 2004-06-13
  • 打赏
  • 举报
回复
我想我可以写的出来,在学校里我痛苦地接受了一个多月的sql强化,看来还是有用
netying 2004-06-13
  • 打赏
  • 举报
回复
mark.
internetcsdn 2004-06-13
  • 打赏
  • 举报
回复
我太懒了,

楼主能提供表结构与数据的脚本吗?
haonanernet 2004-06-13
  • 打赏
  • 举报
回复
up
haonanernet 2004-06-13
  • 打赏
  • 举报
回复
懒猫可是一点都不懒呀!
因为sql语句本来答案就不唯一,有些可以用嵌套也可以用联结等等
懒猫写的几乎都对,有点和答案不同的我给写到下面:
(1) select * from score where cno='3-105' and grade between 60 and 80
(3)select count(*) from student where class='95031'
(5)select sno from score group by sno having min(grade) >70 and max(grade) <90 //having 后面好象不用加()的吧
(6)select avg(grade) from student a,score b where a.sno=b.sno and a.class='95033' group by b.cno //这里同学号应该分组
(17)select a.sno,a.cno,a.grade from score a where grade=(select max(grade) from score b where a.cno=b.cno);我觉得这样写简单些
另外请大家帮我看看
http://community.csdn.net/Expert/TopicView2.asp?id=3084133&datebasetype=now
谢谢!

diangong 2004-06-13
  • 打赏
  • 举报
回复
速度好快呀
wzh1215 2004-06-13
  • 打赏
  • 举报
回复
---欢迎指正

(1)查询选修课程号"3-105"且成绩在60-80之间的所有记录
select a.* from score a,course b where a.cno=b.cno and a.cno='3-105' and grade between 60 and 80
(2)查询选修课成绩为85,86或88的成绩记录
select * from score where grade in(85,86,88)
(3)查询'95031'班的学生人数
select sum(1) from student where class='95031'
(4)查询至少有5名学生选修的并以'3'开头的课程的平均成绩
select cno,avg(grade) from score where cno in(
select cno from score where sno like '3%' group by cno having count(*)>=5)
(5)查询最底分大于70,最高分小于90的学生学号
select distinct sno from score group by sno having (min(grade)>70 and max(grade)<90)
(6)查询'95033'班学生所选课程的平均分
select avg(grade) from student a,score b where a.sno=b.sno and a.class='95033'
(7)查询选修"3-105"课程的成绩高于'109'号同学成绩的所有学生学号
select sno from score where cno='3-105' and grade>(select grade from score where cno='3-105' and sno='109')
(8)查询与学号为'108'的同学同岁的所有学生的sno,sname和age
select sno,sname,age from student where age=(select age from student where sno='108')
(9)查询"张飞"老师任课的课程号,选修其课程的学生学号和成绩
select sno,grade from score a,course b,teacher c where a.cno=b.cno and b.tno=c.tno and c.tname='张飞'
(10)查询某课程的学生人数多与5人的教师姓名
select c.tname from score a,course b,teacher c where a.cno=b.cno and b.tno=c.tno group by c.tname having count(*)>5
(11)查询"计算机系"与"电子工程系"的教师姓名与职称
select tname,prof from teacher where depart in('计算机系','电子工程系')
(12)查询选修编号为"3-105"课程且成绩至少高于选修编号为"3-245"的同学的
cno,sno,grade并按成绩从高到低排列
select cno,sno,grade from score where cno='3-105' and grade>(select max(grade) from score where cno='3-245')
(13)列出所有同学和教师的"姓名",sex,age
select sname as '姓名',sex,age from student
union all
select tname,sex,age from teacher
(14)查询成绩比该课程平均成绩低的学生的学号,成绩
select a.sno,grade from score a,
(select cno,aver=avg(grade) from score group by cno)b
where a.cno=b.cno and a.grade<b.aver
(15)列出至少两名男生的班号
select class from student where sex='男' group by class having count(*)>=2
(16)查询不姓"王"的学生姓名
select sname from student where sname not like '王%'
(17)查询每门课最高分学生的sno,cno,grade
select a.sno,a.cno,a.grade from score a,
(select cno,maxgrade=max(grade) from score group by cno) b
where a.cno=b.cno and a.grade=b.maxgrade
(18)查询与"李朋"同性别并同班的学生姓名
select a.sname from student a,
(select sex,class from student where sname='李朋') b
where a.sex=b.sex and a.class=b.class
(19)查询"男"教师姓名及其讲授的课程名
select a.tname,b.cname from teacher a,course b where a.tno=b.tno and a.sex='男'
(20)查询选修"计算机导论"课程的"男"同学的成绩表
select a.sno,a.grade from score a,student b,course c
where a.sno=b.sno and a.cno=c.cno and c.cname='计算机导论' and b.sex='男'
zslhfdyx 2004-06-13
  • 打赏
  • 举报
回复
(1) select * from score where cno='3-105' and grade>=60 and grade<=80
(2) select * from score where grade in(85,86,88)
(3) select count(*) from student where class='95031'
(4) select avg(grade),cno from score group by cno having count(sno)>5
(5) select sno from score group by sno having min(grade)>70 and max(grade)<90
zjcxc 元老 2004-06-13
  • 打赏
  • 举报
回复
不太难,供大家练习倒是非常有用.

大家可以用它来检验一下自己的SQL水平
leeboyan 2004-06-13
  • 打赏
  • 举报
回复
不会,等高手来
zslhfdyx 2004-06-13
  • 打赏
  • 举报
回复
up...
applebook 2004-06-13
  • 打赏
  • 举报
回复
跟踪!!!!!!!!!!!
Dogfish 2004-06-13
  • 打赏
  • 举报
回复
简单
outwindows 2004-06-13
  • 打赏
  • 举报
回复
有时间再写...
clkun 2004-06-13
  • 打赏
  • 举报
回复
Mark

34,591

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧