Oracle内连接和外连接

/*
种类:
1.内联结(inner join): 仅当至少有一个同属于两表的行符合联接条件时,内联接才返回行. 内联接消除与另一个表中任何不匹配的行.
2.外联结:
外联接会返回from子句中提到的至少一个表的所有行,只要这些行符合任何where或having条件。
左外联接 left join:左边表中所有的行,右边表中没有的字段用null代替。
右外联接 right join:右边表中所有的行, 左边表中没有的字段用null代替。
完整外联结 full join:两表数据都返回,没有的地方用null代替。
3.交叉联结(cross join)
*/

–内联结语法:select 列名 from 表名 inner join 表名2 on 联结条件
–案例: 显示课程名及成绩
select cname ,score from score sc inner join course co on sc.courseid=co.cid;

–左外联接语法: select 列名 from 表名1 left join 表名2 on 联结条件
–特点: 优先查出表名1中所有的符合条件的数据,如果表名2中没有这个对应的数据,则用nULL填充值
–案例描述: 学生表中有一位学员1号’赵柳’,没有参加过任何考试,下面请查出所有没有参加过任何考试的学员
select * from stuInfo st left join score sc on st.stuid=sc.stuid where score is null;

–右外联接与左外联接正好相反:

select * from stuInfo st right join score sc on st.stuid=sc.stuid where score is null;
–完整外联接
select * from stuInfo st join score sc on st.stuid=sc.stuid ;

–交叉联接:左表中的每一行与右表中的每行都组合成。笛卡尔乘积=左表数据行*右表数据行
select * from stuInfo cross join score;
select * from dept;
select * from emp;
select * from dept cross join emp;
–right join:以right join关键字右边的表为基准表,也就是右边表中有的数据全部显示,
–左边表中没有对应的数据时,显示为空
select * from stuInfo st right join score sc on st.stuid=sc.stuid;
insert into stuInfo values (seq_stuid.nextval,‘周星驰’,22,null,‘F’);
–显示学生姓名,课程名及成绩

select sname,cname,score from stuInfo st inner join score sc on st.stuid=sc.stuid inner join course co on sc.courseid=co.cid;

–查询所有学生的姓名、课程名和成绩,如果该生没有参加过任何考试,也需显示出来
select sname,cname,score from stuInfo st full join score sc on st.stuid=sc.stuid full join course co on sc.courseid=co.cid;

–查询所有学生的姓名、课程名和成绩,如果该生没有参加过任何考试,也需显示出来
–如果该课程没有任何人选修,也需显示

–查出学员在’Oracle’这门课程中的总成绩和平均成绩,最高分数,最低分数
select ‘连接查询’,sum(score),avg(score),max(score),min(score) from score sc, course co where sc.courseid=co.cid and co.cname=‘Oracle’;
select ‘嵌套查询’,sum(score),avg(score),max(score),min(score) from score sc where sc.courseid=(select cid from course where cname=‘Oracle’);
select ‘内联查询’,sum(score),avg(score),max(score),min(score) from score sc inner join course co on sc.courseid=co.cid where co.cname=‘Oracle’;
–查询每门课程的平均成绩,显示课程名称,平均分
select cname,sum(score),avg(score),max(score),min(score) from score sc ,course co where sc.courseid=co.cid group by cname;

–查出学员在’html网页设计’这门课程中不及格的学生人数
select count(*) 不及格人数 from course co ,score sc where sc.courseid=co.cid and co.cname=‘html网页设计’ and sc.score<60;
–查询这样的学生信息?->成绩表中score<60
–查询出有不及格记录的学生学号
select stuid from course co,score sc where co.cid=sc.courseid and sc.score<60 ;

–查询出有’html网页设计’不及格记录的学生学号
select stuid from course co,score sc where co.cid=sc.courseid and sc.score<60 and co.cname=‘html网页设计’;

–查询这些学生的详细信息

select * from course co,score sc where co.cid=sc.courseid and sc.score<60 and co.cname=‘html网页设计’;

–获取参加html考试学生的人数
select count(*) 参加HTML考试的人数 from course co,score sc where co.cid=sc.courseid and co.cname=‘html网页设计’;

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值