三表查询sql

SNO SNAME SEX DEPTNO BIRTHDAY
-------- -------------------- --- -------------------- -----------
97001 张三 女 计算机系 1990-2-9
97002 李四 男 计算机系 1990-11-11
97003 王五 男 管理系 1991-3-9
97004 小明 男 计算机系 1990-5-10
97005 大军 男 机械系 1991-2-9
97101 圆圆 女 机械系 1990-8-9

6 rows selected

SQL> select * from course;

CNO CNAME CREDIT CPNO
----- -------------------------------------------------- ------ -----
1 计算机语言_pascal 6.0
2 操作系统_dos 6.0 1
3 数据库原理 6.0 2
4 编译原理 6.0 1

SQL> select * from sc;

SNO CNO SCORE
-------- ----- -----
97001 1 90
97002 2 89
97002 3 89
97003 3 95
97003 1
3) 列出“计算机”系选修了编号为“2”的课程的学生的学号和姓名;
4) 列出选修了学分大于3,并且成绩不及格的学生的学号、姓名、课程编号;
5) 列出“计算机系”、“管理系”、“机械系”三个系的学生成绩在85分以上的选修课程编号、成绩;

跪求,速度...
6) 列出所有学生的的学号、姓名、不及格的课程编号及具体成绩;
7) 分课程统计成绩在85分以上的学生的人数,列出课程编号、课程名称和学生人数;
8) 列出“计算机”系每个学生不及格的课程门数;
9) 列出平均成绩在85分以上的学生的学号和姓名;
10) 列出选修人数小于5的课程的编号和实际选修人数;
11) 列出同时选修了编号为“2”、“ 3”两门课程的学生的学号,姓名;
12) 列出没有任何一门课成绩不及格的学生的学号、姓名;
13) 列出“2”课程得分最高的学生的学号,姓名,成绩;
14) 列出既有成绩不及格又有成绩在85分以上的学生的学号,姓名;
15) 列出选修的课程学分都大于3的学生的学号、姓名;
我只有十分了...

看看我以前做过的经典例题
student(sno,sname,sex,age,deptno) --学生表
department(dno,dname) --系院表
teacher(tno,tname) --教师表
course(cno,cname,teachno) --课程表
score(id,stuno,corsno,score) --成绩表
--一检索学习jsp这门课程的学生,列出学生的名字
select student.sname
from student
where sno in(select stuno from score where corsno =(select cno from course where cname='jsp'))
go

select student.sno
from student inner join score on student.sno=score.stuno
inner join course on corsno=course.cno
where course.cname='jsp'

select sname
from student
where sno in(select score.stuno
from score inner join course on score.corsno=course.cno where course.cname='jsp')

--二检索java课程有多少学生学习.
select COUNT(stuno) as 'JAVA学习人数'
from score
where corsno =(select cno from course where cname='java')

--三检索各科课程各有多少学生学习,
select course.cname,COUNT(score.stuno) as '学习人数'
from score inner join course on score.corsno=course.cno
group by course.cname

--四检索学习了jsp和servlet两门课程的学生有多少
select COUNT(sno) as 'jsp和servlet课程的学生人数'
from student
where sno in(select score.stuno
from score inner join course on score.corsno=course.cno
where course.cname='jsp') and
sno in(select score.stuno
from score inner join course on score.corsno=course.cno
where course.cname='jsp')

--五检索学习了超过两门课程的学生有多少
select corsno,COUNT(stuno) as '学习超过两门课程的学生人数'
from score
group by corsno having COUNT(stuno)>2 order by corsno

--六检索重来没有学生学习过的课程,课程的名字
select cname
from course
where cno not in(select corsno from score )
--七检索老师A有多少学生
select COUNT(stuno) as '张老师的学生人数'
from score
where corsno in(select course.cno
from course inner join teacher on course.teachno=teacher.tno
where teacher.tname='张华' )

--九检索一共有多少老师,每一个老师所授课程是什么
select teacher.tno,course.cname
from course join teacher on course.teachno=teacher.tno
group by teacher.tno,course.cname

--十检索每个老师有多少个学生
select teacher.tno,teacher.tname ,COUNT(score.stuno) as '学生人数'
from teacher inner join course on teacher.tno=course.cno
inner join score on course.cno=score.corsno
group by teacher.tno,teacher.tname order by teacher.tno

--十一检索授课超过两门的老师
select teacher.tno,COUNT(course.cno) as '授课超过两门的老师'
from teacher inner join course on teacher.tno=course.teachno
group by teacher.tno having(COUNT(course.cno)>2) order by teacher.tno

--十三检索A老师所授课程被学生全部学习的学生的名字.
select sname
from student
where sno (select stuno from score inner join )

--查询选修人数超过人的课程的名字,以及每门课的选课总人数,并将结果按照人数的升序排序
select teacher.tno,COUNT(score.stuno) as '选课总人数'
from teacher inner join course on teacher.tno=course.teachno
inner join score on course.cno=score.corsno
group by teacher.tno having(COUNT(score.stuno)>350) order by count(score.stuno) asc

--查询每门课的成绩都比这门课的其他同学高的学生的学号
select stuno
from score
where
group by corsno
--查询每个同学的学号和姓名以及这个同学成绩为优秀的课程的门数。
select student.sno,student.sname,max(score.score)
from student inner join score on student.sno=score.stuno
group by student.sno,student.sname

--查询其他系的同学的年龄比‘软件工程系’的某个学生的年龄小的学生的学号,姓名和系别
--查询每个系中年龄高于这个系的平均年龄的学生的学号和姓名、年龄
select sno,sname,age
from student
group by deptno,sno,sname,age having(age>AVG(age))

--查询没有选课的学生的学号和姓名
select sno,sname
from student
where sno not in(select sno from score)
go

--查询每个同学成绩高于自己选修课程的平均分的学生的学号和选修课程的课号
select stuno,corsno
from score
group by stuno,corsno,score having(score>AVG(score))
--查询每个学生以及选修课程的情况(要求使用做外连接)
select student.sno,student.sname,course.cname
from student inner join score on student.sno=score.stuno
left outer join course on score.corsno=course.cno
order by student.sno
go
--自己举例实现带有ANY和ALL谓词的例子,各举一个例子
--查询其他系中比天文系所有学生年龄都小的学生姓名及年龄。
select sname,age,deptno
from student
where age<any(select student.age
from student inner join department on student.deptno=department.dno
where department.dname='天文系') and
deptno <>(select dno from department where dname='天文系')
go

--查询全体学生的信息,查询结果按所在系的系名升序排列,同一系的学生按年龄降序排列。
select student.sno,student.sname,department.dname,student.sex,student.age
from student,department
where student.deptno = department.dno
order by department.dname ASC,student.age DESC
go
--查询选修课门数等于或大于门的学生的平均成绩和选课门数。
select stuno,convert(numeric(8,2),AVG(score)) as '平均成绩',COUNT(corsno) as '选课门数'
from score
group by stuno having(COUNT(corsno)>2) order by stuno
go
--查询计算机系修数据结构课程的学生的修课成绩,要求列出学生姓名、课程名和成绩。
select student.sname,course.cname,score.score
from student inner join score on student.sno=score.stuno
inner join course on score.corsno=course.cno
where course.cname='c199' order by student.sname
go
--查询学生的选课状况,包括选了课程的学生和没有选课的学生。
select student.sname,count(score.corsno) as '选课状况'
from student left join score on student.sno=score.stuno
group by student.sname
go

--查询选修了“C06”课程,且成绩高于此课程平均成绩的学生学号和成绩。
select stuno,corsno,score
from score
where corsno=(select cno from course where cname='c100') and
score >
--删除计算机系所有不及格学生的选课记录。
select id,stuno,corsno,score
from score
where score<10
go
--创建计算机系学生的选课视图view001,包括学生号、姓名、性别、年龄、系、课程号、课程名及选课成绩。
create view viewScore
as
select student.sno,student.sname,student.sex,student.age,department.dname,course.cno,course.cname,score.score
from student,department,score,course
where student.deptno = department.dno and student.sno=score.stuno and score.corsno = course.cno order by student.sno
go
--在学生表的年龄列上建立一索引,索引名字为ix_age。
create index ix_age on student(age)
go
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-05-11
额,1分1题,好廉价啊。。。
看下下面的代码吧
3)select sno,sname from stu a where a.deptno='计算机系' and a.sNO IN(SELECT SNO FROM SC WHERE CNO=2)
4)SELECT A.SNO,A.SNAME,B.CNO FROM STU A,COURSE B,SC C WHERE B.CREDIT>3 AND C.SCORE<60
AND A.SNO=C.SNO AND B.CNO=C.CNO;
5)SELECT a.sno,a.sname,a.deptno,b.cno,b.score from stu a,sc b
where a.sno=b.sno and a.deptno in('计算机系','机械系','管理系') and b.score>85;
6)select a.sno,a.sname,b.cno,b.score from stu a,sc b where a.sno=b.sno and b.score<60;
7)select b.cno,a.cname,count(1) from course a,sc b where a.cno=b.cno and b.score>85 group by b.cno,a.cname
8)select a.sname,count(1) from stu a,sc b where a.sno=b.sno and b.score<60 and a.deptno='计算机系' group by a.sname;
9)select a.sno,a.sname from stu a,sc b where a.sno=b.sno having avg(score)>85 group by a.sno,a.sname
10)select cno,count(1) from sc having count(1)<5 group by cno;

11)select e.sno,e.sname from (select a.sno,a.sname from stu a,sc b where a.sno=b.sno and b.cno=2) e,
(select c.sno,c.sname from stu c,sc d where c.sno=d.sno and d.cno=3) f
where f.sno=e.sno and f.sname=e.sname;

12)select a.sno,a.sname from stu a where a.sno not in (select sno from sc where score<60);
13)select a.sno,a.sname,b.score from stu a,sc b where a.sno=b.sno and b.cno=2 and b.score=(select max(score) from sc where cno=2);
14)select sno,sname from stu where sno in
(select c.sno from (select * from sc where score>85) c, (select * from sc where score<60) d
where c.sno=d.sno);
15)select a.sno,a.sname from stu a where a.sno not in(select sno from sc where sno in (select sno from course where credit<=3));本回答被提问者采纳
第2个回答  2010-05-11
多加点分就给你写。。。5分15个题 推少了点
相似回答