连接查询

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


--等值连接
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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蜗牛互联网

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值