标签:使用 from 一个 nes 员工信息 最大 name one style
问题提出
1、where 里的子查询
例:查询工资比7654高的员工编号,姓名,工资。
select * from emp where empno=7654;
select sal from emp where empno=7654;
select empno,ename,sal from emp where sal>(select sal from emp where empno=7654);
2、在 from 里使用 表
例:查询20,30部门中工资大于2000的员工信息
select * from emp where deptno in(20,30);
select * from (select * from emp where deptno in(20,30)) where sal>2000;
3、in 的用法
例:和SMITH或者JONES在同一部门,同一职位工作的员工有哪些。
select * from emp where ename in(‘SMITH‘,‘JONES‘);
in 结合内求东西
in(任何结果集)
select * from emp where (deptno,job) in (select deptno,job from emp where ename in(‘SMITH‘,‘JONES‘));----子查询列名要和主句列名一致
4、any 的用法
=any 与in一样
select * from emp where (deptno,job) =any (select deptno,job from emp where ename =any (‘SMITH‘,‘JONES‘));
>any 只要大于子查询中任何一个值即可
select * from emp where sal>any (select min(sal) from emp group by deptno);
<any 只要小于子查询中任何一个值即可
select * from emp where sal<any (select max(sal) from emp group by deptno);
5、all 的用法
查询每个部门最低的工资
select deptno,min(sal) from emp group by deptno;
>all 比最大值大
例:求工资大于每个部门最低工资的员工信息
select * from emp where sal>all (select min(sal) from emp group by deptno);
<all 比最小值小
例:求工资小于每个部门最高工资的员工信息
select * from emp where sal<all (select max(sal) from emp group by deptno);
例题
查询CLERK的员工编号,姓名,部门名称,和所在部门人数--用两种做法 分组,多表,子查询
select deptno,count(*) from emp where job=‘CLERK‘group by deptno;
select * from emp;
方法一:
select empno,ename,dname,c.x
from emp e,dept d,(select deptno,count(*) x from emp where job=‘CLERK‘ group by deptno) c
where e.deptno=d.deptno and e.deptno=c.deptno and job=‘CLERK‘;
方法二:
select * from(select empno,ename,dname,e.deptno from emp e,dept d
where e.deptno=d.deptno and job=‘CLERK‘ ) a,
(select deptno,count(*) x from emp where job=‘CLERK‘ group by deptno) b
where a.deptno=b.deptno
方法三:
select e.ename,d.dname,c.x
from emp e,dept d,
(select a.deptno,count(*) x from emp a,dept b where a.deptno=b.deptno and job=‘CLERK‘ group by a.deptno )c---------此步骤较为复杂
where e.deptno=d.deptno and d.deptno=c.deptno and e.job=‘CLERK‘;
使用分析函数
select empno ,ename,dname,count(*) over( partition by a.deptno)
from emp a,dept b
where a.deptno=b.deptno
and job=‘CLERK‘
标签:使用 from 一个 nes 员工信息 最大 name one style
原文地址:https://www.cnblogs.com/wyx666/p/11217882.html