连接查询

连接查询,会产生笛卡尔积,有时会降低运行效率。


--等值连接
select * from emp e,dept d
where e.deptno=d.deptno


--查询ACCOUNTING部门下有哪些员工


--使用连接查询
select  * from emp e,dept d
where e.deptno=d.deptno and d.dname='ACCOUNTING'


--使用嵌套子循环
select * from emp where deptno=(select deptno from dept where dname='ACCOUNTING')


--查询所有员工的工资等级
select * from emp e,salgrade s
where e.sal between s.losal and s.hisal


--查询各个工资等级的人数
select s.grade as 工资等级,
       count(s.grade) as 该等级人数 from emp e,salgrade s
where e.sal between s.losal and s.hisal
group by s.grade


--查询每个部门每种工资等级的人数
select d.dname as 部门名称,
       s.grade as 工资等级,
       count(*) as  人数 from emp e, dept d, salgrade s
where e.deptno=d.deptno and e.sal between s.losal and s.hisal
group by d.dname,s.grade
order by d.dname,s.grade




--外连接 左外连接  右外连接
         --左外连接  以emp为左表
select * from emp e
left join dept d on e.deptno=d.deptno
         --左外连接  以dept为左表
select * from dept d
left join emp e on e.deptno=d.deptno
--什么时候用外连接
select * from dept d
left join emp e on e.deptno=d.deptno
where e.deptno is null




--自连接
select * from emp e,emp e1
where e.mgr=e1.empno


--显示各个经理带了几个员工
select mgr, count(*) from emp
where mgr is not null
group   by mgr


--显示各个经理名字
select e1.ename from emp e,emp e1
where e.mgr=e1.empno and e1.mgr is not null
group by e1.ename


--显示不重复元素
select distinct job from emp


全部评论

相关推荐

05-19 19:57
蚌埠学院 Python
2237:Gpa70不算高,建议只写排名,个人技能不在多而在精,缩到8条以内。项目留一个含金量高的,减少间距弄到一页,硕士简历也就一页,本科不要写很多
实习,投递多份简历没人回...
点赞 评论 收藏
分享
流浪的神仙:无恶意,算法一般好像都得9硕才能干算法太卷啦
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务