(1)左外连接,列出左边所有元组 from student left join sc on (student.sno=sc.sno)
(2)右外连接,列出右边所有元组
(3)全连接,from A outer join B on A.key=B.key
4. 多表连接(where或者from)
例:查询每个学生的学号、姓名、选修的课程名及成绩
select student.sno,sname,cname,grade
from student,sc,course
where student.sno=sc.snoand sc.cno=course.cno
或者
select student.sno,sname,cname,grade
from student join sc on student.sno=sc.sno
join course on sc.cno=course.cno
select sname,cno,grade into t1 from student,sc
where student.sno=sc.sno and dept='计算机系'
select * from t1
drop table t1
(2)查询“信息管理系”选修了“计算机文化学”课程的学生姓名和成绩,查询结果存入新表t2。
[SQL] 纯文本查看复制代码
select sname,grade into t2 from student,sc,course
where student.sno=sc.sno and course.cno=sc.cno and cname='计算机文化学' and dept='信息管理系'
select * from t2
drop table t2
(3)查询所有选修了“Java”课程的学生情况,列出学生姓名和所在系,查询结果存入新表t3。
[SQL] 纯文本查看复制代码
select sname,dept into t3 from student join sc on student.sno =sc.sno
join course on sc.cno=course.cno
where course.cname='Java'
select * from t3
drop table t3
(4)统计每个系的学生的考试平均成绩,列出系名和平均成绩,查询结果存入新表t4。
[SQL] 纯文本查看复制代码
select dept '系名',AVG(grade) '平均成绩' into t4 from student join sc on student.sno=sc.sno
group by dept
select * from t4
drop table t4
select student.sno '学生学号',COUNT(sc.cno) '选课门数',AVG(grade) '平均成绩',MAX(grade) '最高成绩',MIN(grade) '最低成绩'
into t5
from student left join sc on student.sno=sc.sno
group by student.sno
select * from t5
drop table t5
(6)查询与“刘晨”在同一个系学习的学生的姓名和所在的系,查询结果存入新表t6。
[SQL] 纯文本查看复制代码
select st2.sname '姓名',st2.dept '所在系' into t6 from student st1,student st2
where st1.sname='刘晨'
and st2.sname<>'刘晨'
and st1.dept=st2.dept
select * from t6
drop table t6
(7)查询与“数据结构”在同一个学期开设的课程的课程名和开课学期,查询结果存入新表t7。
[SQL] 纯文本查看复制代码
select c2.cname '课程名',c2.semester '开课学期' into t7 from course c1,course c2
where c1.cname='数据结构'
and c2.cname<>'数据结构'
and c1.semester=c2.semester
select * from t7
drop table t7
(8)查询至少被两个学生选的课程的课程号,查询结果存入新表t8。
[SQL] 纯文本查看复制代码
select cno into t8 from sc
group by cno
having COUNT(sno)>=2
select * from t8
drop table t8
select student.sno '学号',COUNT(cno) '选课门数' into t12 from student left join sc on student.sno=sc.sno
where dept='计算机系'
group by student.sno
select * from t12
drop table t12
select student.sno '学号',COUNT(cno) '选课门数' into t13 from student left join sc on student.sno=sc.sno
where dept='信息管理系'
group by student.sno
having COUNT(cno)<3
order by COUNT(cno)
select * from t13
drop table t13